0

I'm new to Jquery and I want to change the 2000 while the code is running

var interval = setInterval(function() {
//do something
}, 2000);

Something like this:

var wait = 200;
var interval = setInterval(function() {

if(wait == 200)
{
wait = 100;
}

}, wait);

Is there a way of doing this?

LordBunny
  • 3
  • 2
  • 5
    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) – Developer Jan 07 '17 at 16:45
  • Oriel.F's link should do it. If you're not prepared to wait for the next call to the anonymous function before changing the timeout, then you'll have to do something slightly clever with it, probably involving `clearTimeout`. – David Knipe Jan 07 '17 at 18:30
  • Can you stop the loop and start again or is that a problem? `clearInterval(interval)` then `setInterval(fn, newTime)` – BrunoLM Jan 07 '17 at 19:40

1 Answers1

1

You can stop the loop

clearInterval(interval);

And start over with the new time:

interval = setInterval(fn, newTime);
BrunoLM
  • 97,872
  • 84
  • 296
  • 452