Search code examples
javascriptjquerytimeoutresponsive-slides

responsiveslides.js - Assign specific timeout to each slide


I created a slider with responsiveslides.js. It contains 5 different image slides.

I usually can assign a timeout of, for example, 4000 milliseconds, but it will be assigned to every single image of the slider.

I would like to assign different timeouts to each image slide. For example:

slide 1: timeout = 10000
slide 2: timeout = 2000
slide 3: timeout = 4000
slide 4: timeout = 4000
slide 5: timeout = 2000

How can I obtain that? Is it possible? Thank you very much for your help and sorry for my poor english.

I edited the code as suggested by Timctran but I did something wrong. Can you help me identifying what's wrong in this code?

I tried to change the code as you suggested. I don't have experience in javascript code so I did something wrong and the slider doesn't work anymore, so I reverted to old version. Here is how i modified the code:

 // Array to enter timeout values.
        var desiredTimeouts = [10000, 2000, 4000, 4000, 2000];
    // Auto cycle
    if (settings.auto) {
        startCycle = function (i) {
            rotate = setTimeout(function () {
          // Clear the event queue
          $slide.stop(true, true);

          var idx = index + 1 < length ? index + 1 : 0;

          // Remove active state and set new if pager is set
          if (settings.pager || settings.manualControls) {
            selectTab(idx);
          }

          slideTo(idx);
        startCycle(index);
    }, desiredTimeout[i]);
};

      // Init cycle
      startCycle(index);
    }

    // Restarting cycle
    restartCycle = function () {
      if (settings.auto) {
        // Stop
        clearTimeout(rotate);
        // Restart
        startCycle(index);
      }
    };

Any help in correcting the code will be really appreciated. Thanks!


Solution

  • There's a function in the responsiveslides.js called rotate (line 232). In that part of the code, make any changes you see here:

    // Array to enter timeout values.
    var desiredTimeouts = [10000, 2000, 4000, 4000, 2000];
    // Auto cycle
    if (settings.auto) {
      startCycle = function (i) {
        rotate = setTimeout(function () {
          // Clear the event queue
          ...
          slideTo(idx);
          startCycle(index);
        }, desiredTimeout[i]);
      };
      // Init cycle
      startCycle(index);
    }
    ...
         clearTimeout(rotate);
         startCycle(index);
    ...
         clearTimeout(rotate);
    

    Summary of changes made to script:

    1. Add a line which contains desiredTimeouts.
    2. Changed setInterval to setTimeout.
    3. Add a call to startCycle at the end of the function.
    4. Changed two instances of clearInterval to clearTimeout
    5. Made index called as an argument to startCycle.