Thursday, February 14, 2019

Stop setInterval call in JavaScript



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




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


Answer



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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...