Reputation: 13534
The timing value in setInterval
determines the next time at which the function is going to be invoked again. For example:
t = 8000;
myFunction(){
if (someCondition){
//halt setInterval
//t = 5000;
// setInterval(myFunction,t);
}
// do some logic
}
setInterval(myFunction, t);
The problem is changing of t
will not affect the time that I want the current call to be remained before the next call. In other words, is there any way to halt setInterval
and then I can reset it again with new time?
Upvotes: 0
Views: 377
Reputation: 413712
If you want to do something like that, you're better off doing your own interval control with setTimeout
.
var shortInterval = 2000, longInterval = 4000;
function timerCode() {
// do something
if (whatever) {
setTimeout(timerCode, shortInterval);
}
else {
setTimeout(timerCode, longInterval);
}
}
setTimeout(timerCode, longInterval);
That way, on each iteration of the timer you can decide how long it should be until the next iteration. If you want to stop, just don't reset the timer at all.
Upvotes: 3
Reputation: 136
You have to use clearInterval().
var myInterval = setInterval(myFunction, t);
...
clearInterval(myInterval);
...
myInterval = setInterval(myFunction, 1000);
Upvotes: 1