tags:

views:

9581

answers:

4

I am using setInterval(fname, 10000); to call a function every 10 secs in javascript. Is it possible to stop calling the calling on some event?

I want the user to be able to stop the repeated refresh of data.

+34  A: 

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().

John Millikin
+5  A: 

if you setup the return of setInterval to a variable you can use clearInterval to stop it.

var myTimer = setInterval(...);
clearInterval(myTimer);
Quintin Robinson
A: 

the question was if we can stop repeating the action by some event. or by some condition. for example repeat something 10 times only.

max
A: 

you can set a new variable and have it increment ++ (count up one) every time it runs, then use a conditional statement to end it

var varCounter = 0;
var varName = function(){
     if(varCounter <= 10) {
          varCounter++;
          /* your code goes here*/
     } else {
          clearInterval(vanName);
};

$(document).ready(function(){
     setInterval(vanName, 10000);
});

I hope that helps... also I hope thats right :P

OMGrant