When working on some Javascript for a web application, I noticed that I had used setTimeout
, but I had tried to clear it with clearInterval
and it stopped the timeout from occurring in Google Chrome and Internet Explorer 9.
Are clearTimeout
and clearInterval
interchangeable?
Here's a JSfiddle with an example of what I'm talking about.
clearTimeout(timeout)Clears an existing timer by passing in the numeric value returned by setTimeout(). Sometimes you need to clear a timeout timer before it executes. clearTimeout() uses the value returned by the setTimeout(function, milliseconds) function.
The clearTimeout function will not reset the number, that is why you have to set your timerID to null when you want to reuse this variable.
The global clearTimeout() method cancels a timeout previously established by calling setTimeout() .
You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens. It's usually more practical to use clearInterval with setInterval because setInterval usually runs indefinitely.
Actually I believe that we can make a fairly strong conclusion from the W3C spec (http://www.w3.org/TR/html5/webappapis.html#timers). It is not explicitly guaranteed but we have a lot of evidence that almost any reasonable implementation would have this behavior:
1) Timeouts and Intervals actually use the same underlying function:
The setTimeout() method must return the value returned by the timer initialization steps, passing them the method's arguments... and the repeat flag set to false.
The setInterval() method must return the value returned by the timer initialization steps, passing them the method's arguments.... and the repeat flag set to true.
2) This single function - the "timer initialization steps" mentioned above - uses a single list of timers:
2, ...let handle be a user-agent-defined integer that is greater than zero that will identify the timeout to be set by this call in the list of active timers.
10, Return handle...
3) clearTimeout() and clearInterval() both operate on that list (and in fact are not differentiated by the spec in any way)
The clearTimeout() and clearInterval() methods must clear the entry identified as handle from the list of active timers of the WindowTimers object on which the method was invoked, where handle is the argument passed to the method, if any. (If handle does not identify an entry in the list of active timers of the WindowTimers object on which the method was invoked, the method does nothing.)
I believe this presents a fairly strong case that clearTimeout and clearInterval should by synonymous according to the spec. This is backed up by the fact that this works in all the browsers I tested (Chrome 37, Firefox 33, and Opera 25).
No, they are not interchangeable.
Sure, some browsers may very well share the same code to clear intervals and timeouts the same way, but does not mean they are interchangeable and you are most certainly not guaranteed that they would work the same across all browser implementations. It comes down to these two methods being defined differently for different purposes and therefore you should use them for their designated uses.. Otherwise, you're just asking for trouble.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With