Reputation: 41
I have this basic jquery script here:
var delay = 200;
setInterval(function(){
countup += 1;
if(countup >= 50) {
console.log('SSS');
console.log(delay);
delay = 10000; //Set the delay slower
}
console.log(countup);
},delay);
It does set the delay to 10000, but it dosen't update the interVal, it just keeps running at 200 ms.. Any one know what to do here?
Upvotes: 1
Views: 387
Reputation: 433
If you want dynamic timeouts, use setTimeout instead of setInterval.
var delay = 200;
function myInterval(){
countup += 1;
if(countup >= 50) {
console.log('SSS');
console.log(delay);
delay = 10000; //Set the delay slower
}
setTimeout(myInterval, delay);
console.log(countup);
}
setTimeout(myInterval ,delay);
Upvotes: 3
Reputation: 195992
You need to clear the interval and reset it.
var delay = 200,
slowDelay = 10000,
countup = 0,
interval;
function action(){
countup += 1;
if(countup >= 50) {
console.log('SSS');
console.log(delay);
clearInterval(interval);
setInterval(action,slowDelay );
}
console.log(countup);
}
setInterval(action,delay);
Upvotes: 1
Reputation: 43842
You can't change the delay of a setInterval
interval, but you can use setTimeout
instead to repeatedly set a timeout using a changing delay:
var delay = 200;
setTimeout(function f(){
countup += 1;
if(countup >= 50) {
console.log('SSS');
console.log(delay);
delay = 10000;
}
console.log(countup);
setTimeout(f, delay);
},delay);
Upvotes: 0