Are clearTimeout and clearInterval the same?

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

Leave a Comment