How To Stop setInterval From Running?

13 Answers

New Answer


use clearInterval() method to stop. clear explanation see this https://www.w3schools.com/jsref/met_win_clearinterval.asp


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);


Supriya Gangapatnam Thank Ypu So Much!👍


using clearInterval() function example: vai i=setInterval(function_name, time(milliseconds); clearInterval(i) //To stop


You store setInterval(...) in a variable then, use stopInterval(), passing the variable as a parameter


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);


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


Satch Esguerra can we place clearInterval inside setInterval?


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.


You need to clearInterval(); for running setinterval...


Rowsej ah... i get it!👍 Thanks!😊


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); }


Set timing.