+ 12

How To Stop setInterval From Running?

23rd Oct 2018, 12:06 AM
Jingga Sona
Jingga Sona - avatar
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);
23rd Oct 2018, 11:28 AM
Googel
Googel - avatar
+ 5
using clearInterval() function example: vai i=setInterval(function_name, time(milliseconds); clearInterval(i) //To stop
23rd Oct 2018, 2:17 PM
{ sood(Hemant , Prakash); }
{ sood(Hemant , Prakash); } - avatar
+ 4
Supriya Gangapatnam Thank Ypu So Much!👍
23rd Oct 2018, 2:43 AM
Jingga Sona
Jingga Sona - avatar
+ 4
You store setInterval(...) in a variable then, use stopInterval(), passing the variable as a parameter
24th Oct 2018, 3:34 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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);
23rd Oct 2018, 1:38 PM
Seniru
Seniru - avatar
+ 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
23rd Oct 2018, 2:54 PM
jay
+ 3
Satch Esguerra can we place clearInterval inside setInterval?
23rd Oct 2018, 10:04 PM
Jingga Sona
Jingga Sona - avatar
+ 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.
23rd Oct 2018, 10:52 PM
Rowsej
Rowsej - avatar
+ 2
You need to clearInterval(); for running setinterval...
23rd Oct 2018, 4:35 PM
Fahad Ahmad
Fahad Ahmad - avatar
+ 2
Rowsej ah... i get it!👍 Thanks!😊
23rd Oct 2018, 10:55 PM
Jingga Sona
Jingga Sona - avatar
+ 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); }
23rd Oct 2018, 4:42 PM
Virus5600
Virus5600 - avatar
0
Set timing.
24th Oct 2018, 4:44 PM
Alex[]
Alex[] - avatar