Stop setInterval call in JavaScript
setInterval()
returns an interval ID, which you can pass to clearInterval()
:
var refreshIntervalId = setInterval(fname, 10000);/* later */clearInterval(refreshIntervalId);
See the docs for setInterval()
and clearInterval()
.
If you set the return value of setInterval
to a variable, you can use clearInterval
to stop it.
var myTimer = setInterval(...);clearInterval(myTimer);
You can set a new variable and have it incremented by ++ (count up one) every time it runs, then I use a conditional statement to end it:
var intervalId = null;var varCounter = 0;var varName = function(){ if(varCounter <= 10) { varCounter++; /* your code goes here */ } else { clearInterval(intervalId); }};$(document).ready(function(){ intervalId = setInterval(varName, 10000);});
I hope that it helps and it is right.