This question already has an answer here:
I have a global variable called interval, and I need to change this global variable to 5000 inside a function, so after waiting for 1 second, the setInterval function will now wait for 5 seconds. However, when I tried the code below, it only waits 1 second every time it's executed.
var timeToWait1 = 1000;
var timeToWait2 = 5000;
var interval = timeToWait1;
setInterval(function(){ waitFunction () }, interval);
function waitFunction() {
interval = timeToWait2;
} //end of function waitFunction()
Interval is set once and can't be changed, You'd need timeout.
Once an interval has started, you can't change the duration it uses. You'll have to stop the interval and re-start it with the new duration.
You might consider using a recursive
setTimeout
instead, to avoid the need for clearing: