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.
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.
@cnu,
You can stop interval, when try run code before look ur console browser (F12) ... try comment clearInterval(trigger) is look again a console, not beautifier? :P
Check example a source:
Declare variable to assign value returned from setInterval(...) and pass the assigned variable to clearInterval();
e.g.
// Anywhere you've access to timer declared above call clearInterval
You can also stop the interval by adding an event listener to let's say a button with the ID "stop-interval":
HTML:
Note: The interval will still be executed, nothing will happen though.
Already answered... But if you need a featured, re-usable timer that also supports multiple tasks on different intervals, you can use my TaskTimer (for Node and browser).
In your case, when users click for disturbing the data-refresh; you can call
timer.pause()
thentimer.resume()
if they need to re-enable.See more here.
If you set the return value of
setInterval
to a variable, you can useclearInterval
to stop it.Why not use a simpler approach? Add a class!
Simply add a class that tells the interval not to do anything. For example: on hover.
I've been looking for this fast and easy approach for ages, so I'm posting several versions to introduce as many people to it as possible.