Jan 15, 2007 - In this tutorial we'll look at JavaScript's setTimeout, clearTimeout, setInterval and clearInterval methods, and show how to use them to set. Mar 29, 2011 - I want to stop the timed function with clearInterval when the button is clicked, and re-start it (with setInterval) once the button function has.
After we put this function into variable in order to be able to use clearInterval()
, why it works without calling this function
Usually when we put a function into variable, in order to fire it we should call it. In this example I thought it should have worked like this:
But it works without calling it.
Thank you.
3 Answers
It would have worked as you expected if you were actually putting a function in the timer
variable but you are not. You are calling setInterval
function and storing its return value into the timer
variable.
In order for it to work like you expect it to, you need to put it inside another function like this :
Now, calling timer()
will invoke the timer as you expect it to does.
You need to wrap your setInterval to a function.
Not the answer you're looking for? Browse other questions tagged javascriptsetinterval or ask your own question.
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.
10 Answers
setInterval()
returns an interval ID, which you can pass to clearInterval()
:
See the docs for setInterval()
and clearInterval()
.
![Javascript clearinterval inside setinterval Javascript clearinterval inside setinterval](https://i.stack.imgur.com/wnWE6.png)
![Jquery Setinterval Clearinterval Jquery Setinterval Clearinterval](https://i.ytimg.com/vi/zZd5hGFnc9A/maxresdefault.jpg)
If you set the return value of setInterval
to a variable, you can use clearInterval
to stop it.
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:
I hope that it helps and it is right.
The answers above have already explained how setInterval returns a handle, and how this handle is used to cancel the Interval timer.
Some architectural considerations:
Please do not use 'scope-less' variables. The safest way is to use the attribute of a DOM object. The easiest place would be 'document'. If the refresher is started by a start/stop button, you can use the button itself:
Since the function is defined inside the button click handler, you don't have to define it again. The timer can be resumed if the button is clicked on again.
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:
HTML:
Note: The interval will still be executed, nothing will happen though.
The clearInterval() method can be used to clear a timer set with the setInterval() method.
setInterval always returns a ID value. This value can be passed in clearInterval() to stop the timer.Here is an example of timer starting from 30 and stops when it becomes 0.
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.
protected by hjpotter92Sep 23 '13 at 4:47
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?