0

I have the following code running a slideshow. I want to be able to set the duration of the interval from within the function rather than use a static value - depending on other factors I'll add in later. Is this possible? the below does not seem to work.

Thanks

$('#slideshow > div:gt(0)').hide();
setInterval(function() { 
   $('#slideshow > div:first')
      .fadeOut(0)
      .next()
      .fadeIn(0)
      .end()
      .appendTo('#slideshow');
   window.duration = 1000;
 },  duration);
RonyLoud
  • 2,408
  • 2
  • 20
  • 25
BigJ
  • 3
  • 2
  • Possible duplicate of [Changing the interval of SetInterval while it's running](http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running) – freedomn-m Mar 16 '17 at 14:58

1 Answers1

0

You can use setTimeout.

Then you would have to call this function every time in your callback.

Something like

function myCallback() { 
    ...
    ...

    var myTime = 2435; // whatever depending on sth

    setTimeout(myCallback, myTime);
}

setTimeout(myCallback,  someInitialTime);
Chris Dąbrowski
  • 1,902
  • 1
  • 12
  • 12
  • Thanks. But I don't see the difference? my question could equally be applied to setting the duration within the a function in setTimeout? – BigJ Mar 16 '17 at 15:02
  • @BigJ If you use `createInterval` then you have created the interval with a specific duration. Changing the variable that holds the duration at a later point *will not* change the duration that you originally used when you created the `setInterval`. If you use `setTimeout` then it only runs once, which is why you call it again after your callback function has executed. It has the same effect as `setInterval` in that respect, but it means you can modify the duration after every single timeout, if you want to. – Reinstate Monica Cellio Mar 16 '17 at 15:19
  • @BigJ If my answer helped you, please mark it as correct. – Chris Dąbrowski Mar 21 '17 at 20:57