+ 12
How To Stop setInterval From Running?
12 Answers
+ 12
You need to save the setInterval to variable.
For example:
var i=0;
var interv = setInterval(function(){
console.log("Hello");
i++;
if(i>=5) {
clearInterval(interv);
}
} , 1000);
+ 5
using clearInterval() function
example:
vai i=setInterval(function_name, time(milliseconds);
clearInterval(i) //To stop
+ 4
Supriya Gangapatnam Thank Ypu So Much!👍
+ 4
You store setInterval(...) in a variable then, use
stopInterval(), passing the variable as a parameter
+ 3
setInterval returns a unique id when it is initialized. Store it in a variable and use the variable in the clearInterval(id) method
eg:
//initializing
var interval = setInterval(function() {alert("+"),1000);
//stopping
clearInterval(interval);
+ 3
setInterval will return an Id pointing to the function running in background. if u call clearInterval with the Id returned from setinterval, it should remove it
+ 3
Satch Esguerra can we place clearInterval inside setInterval?
+ 3
Jingga Sona You can put it inside the func parameter of setInterval:
var i = setInterval(something, 2000);
var i2 = setInterval(function() { clearInterval(i); }, 5000);
But as you can see, things can get a bit crazy.
+ 2
You need to
clearInterval();
for running setinterval...
+ 2
Rowsej ah... i get it!👍 Thanks!😊
+ 1
You can assign it to a variable and stop it if it met certain condition.
example:
var intrvl = setInterval(myFunc,1000);
if (condition) {
clearInterval(intrvl);
}
0
Set timing.