40

I am writing a check to see if a timeout is active. I was thinking of doing this:

var a = setTimeout(fn, 10);
// ... Other code ... where clearTimeout(a) can be called and set to null
if (a != null)
{
   // do soemthing
}

I was wondering if it would ever be possible that a will be 0. In that case I would use a !== null

Aishwar
  • 9,284
  • 10
  • 59
  • 80

4 Answers4

40

It shouldn't, given this:

handle = window . setTimeout( handler [, timeout [, arguments ] ] )

Let handle be a user-agent-defined integer that is greater than zero that will identify the timeout to be set by this call.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    @Alin: I came to this site via https://developer.mozilla.org/en/DOM/window.setTimeout, where it says that it is part of DOM Level 0. So I thought this actually older than HTML5, but maybe I misinterpreting something. – Felix Kling Oct 15 '10 at 15:24
  • +1 Thanks for the link. You did answer my question, though the accepted answer addressed my concern. – Aishwar Oct 15 '10 at 16:21
  • @Aishwar, but null is not a number, it is not good to mix types, even in js – 4esn0k Oct 12 '12 at 13:42
15

The specifications from Microsoft, Sun and Mozilla just say that it will return an integer. So 0 may be included.

It may be possible (and probable) that some implementations exclude 0 but you shouldn't rely on that. You should go with the !==.

To summarize: Although probably all browser exclude 0 from the IDs returned by setTimeout, you should not write your code with that in mind especially when all your have to do is add an extra =.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
2

Most browsers will return an int starting at 1 and incrementing for each call of setTimeout so in theory it could never be 0.

But keep in mind that this is not really a requirement of the spec, just a convention browsers tend to follow. See the accepted answer here for more details: setInterval/setTimeout return value

Community
  • 1
  • 1
brendan
  • 29,308
  • 20
  • 68
  • 109
  • 4
    It's apparently part of the HTML5 spec (http://www.w3.org/TR/html5/timers.html). Pre-HTML5 it was just a convention. – mhenry1384 Aug 31 '11 at 19:25
2

First: 0 isn't the same as null, (0 == null) would be false in every case';

if you want to test 'a' against something: define 'a' first and later assign the settimeout to 'a'. then check against the type of 'a'. If its 'undefined', the timer hasn't triggered yet

DoXicK
  • 4,784
  • 24
  • 22
  • My mistake :) I didn't check, but yes `0 != null` always. I expected them to be equal in Javascript, considering equalities like '' and 0 - but this is not the case. – Aishwar Oct 15 '10 at 16:17
  • 2
    This does not answer the question. – Grant Gryczan Oct 12 '18 at 19:23
  • @GrantGryczan and yet, back in the days when the dinosaurs were still walking among us, Aishwar accepted it as the answer. While i indeed agree that my answer doesn't answer the title, it did fix the problem. The answer of Felix Kling is however the correct answer. – DoXicK Oct 15 '18 at 13:01