+10

How To Stop setInterval From Running?

10/23/2018 12:06:00 AM

Jingga Sona 🐺 [ON PROJECT]

13 Answers

New Answer

+10

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

+8

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

+4

Supriya Gangapatnam Thank Ypu So Much!👍

+4

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

+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.