0

Reading a previous question about the setInterval method in JavaScript I pointed out on my notebook the following problem:

setTimeout()

works correctly in IE11, FF46 and Chrome51 and so the interval is stopped.

In the MDN documentation, instead, clearTimeout is so described:

Clears the delay set by WindowTimers.setTimeout().

Why? Are clearTimeout and clearInterval interchangable?

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • 1
    If they are, then it's an internal implementation detail and you should definitely not rely on it. – Frédéric Hamidi May 27 '16 at 14:51
  • `clearInterval()` works with `setInterval()` and `clearTimeout()` works with `setTimeout()`. If `clearTimeout()` happens to work with `setInterval()` on some platforms, then that's just an accident of the implementation and not something to be relied upon. – jfriend00 May 27 '16 at 14:52

1 Answers1

0

clearTimeout and clearInterval shouldn't be interchangeable, as they're designed for setTimeout and setInterval, respectively. Besides, setTimeout and setInterval both do different things: setTimeout tells a set of commands to wait until execution and does it only once, while setInterval does that repeatedly, until the end of time (unless you call clearInterval, of course). clearTimeout will cause setTimeout to trigger before the timeout has expired, whilst clearInterval will stop the setInterval loop.

While it may work for one browser, it's not guaranteed to work for all browser implementations, and therefore should be avoided.

NOTE

This is also mentioned in the javascript stackoverflow. Are clearTimeout and clearInterval the same?