43

Two questions:

  1. How is the value returned from setInterval and setTimeout (the ones used to clear the timers) calculated?

  2. Is it possible for both the functions to return the same value during runtime? For example:

    var a = setInterval(fn1, 1000);
    var b = setTimeout(fn2, 1000);

Is it possible for a and b to have the same value?

The first one is more of a for-my-knowledge question, but the second one is more important.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
aditya
  • 1,978
  • 3
  • 17
  • 22

6 Answers6

39

Returns a value which can be used to cancel the timer. So, it would seem unlikely that they return the same value (unless they are reusing values and one of the timers has already been cancelled)

Mozilla states it's DOM level 0, but not part of the specification. (look at the bottom of the page)

I've got an even better reference:

Nabble says:

SetTimeout and setInterval are from the original Javascript specification, pre-ECMA. That specification is not officially standardized anywhere, but it is supported by all web browsers and most implementations of the Javascript language. (Including ActionScript.)

The pre-ECMA specs are often known as the "DOM-0" APIs. Since they have never been standardized before, it makes sense for HTML5 to finally spec the non-deprecated APIs in an attempt to provide a consistent environment across browsers. Especially when recent events have proven that there are companies who like to implement the letter of the standard, but not the spirit.

Read the original spec here, or from Sun (who was an early endorser of JavaScript).

netadictos
  • 7,602
  • 2
  • 42
  • 69
cgp
  • 41,026
  • 12
  • 101
  • 131
  • I wish I could pick two accepted answers. Thanks for the references! – aditya Jun 02 '09 at 15:25
  • 2
    10 years later, in a post-HTML5 world, as per both the [WhatWG](https://html.spec.whatwg.org/#timers) and [W3C](https://www.w3.org/TR/html5/webappapis.html#list-of-active-timers) HTML specs, the ID "must be unique within the list for the lifetime of the object that implements the [WindowOrWorkerGlobalScope mixin]/[WindowTimers interface]". Basically, IDs will never be reused (within one window scope) by any spec-compliant implementation. setTimeout and setInterval use the same ID list (and will not reuse any ID used by the other). – Bob Jan 22 '19 at 03:15
35

Tested this under Opera 9, Safari 3, Firefox 3 and IE 7.

All returned integer values, starting at 1 and then incrementing by 1 for each call to setTimeOut() and setInterval(). However, I noticed that the browsers started the counters and handled them differently:

  • IE started with a (seemingly) random 6-digit number, but subsequent calls to either function incremented this number. After closing and reopening IE I found that the starting number appeared to be randomly generated, as it was nowhere near the count from the previous session.
  • Opera maintained a counter for each tab - closing a tab and opening a new one started the counter from 1 in the new tab.
  • In Safari, the count was global - opening a new tab and calling the functions in different tabs seemed to increment a global reference counter.
  • In Firefox, the counter appeared to start at 2, and incremented on each subsequent call to either function. Like Opera, each tab had its own counter value, but they appeared to all start at 2.

Notice though, that in all the scenarios, no two identifiers (at least in the same tab) are the same.

Alex Rozanski
  • 37,815
  • 10
  • 68
  • 69
  • 11
    I was hoping to find out if the value returned is always truthy, and it seems that that's the case although not specified. Thanks for checking on all those browsers. – w00t Jun 26 '12 at 14:29
7

I think it’s not a standarized behavior. In firefox, it’s just integer, incrementing on each call of setTimeout or setInterval. And, no, they can’t have the same value.

Maciej Łebkowski
  • 3,837
  • 24
  • 32
  • They will *never* have the same value? Regardless of how long the script is run? – aditya Jun 02 '09 at 15:23
  • 1
    It's worth taking a look at my answer for the exact background on how it's supported and whether it's "in the spec" or not. – cgp Jun 02 '09 at 15:25
  • @aditya I think it checks if the id it generates already exists in memory, and generates a new one in the rare case that it does exist. That's what I would do if dealing with random numbers. – Braden Best Nov 12 '12 at 15:25
3

From the Mozilla website:

intervalID is a unique interval ID you can pass to clearInterval().

So it is unique :)

Ropstah
  • 17,538
  • 24
  • 120
  • 194
  • 1
    It might be unique with respect to values returned from multiple setInterval calls, but that doesn't guarantee uniqueness with respect to values returned by setTimeout. – system PAUSE Jun 02 '09 at 15:29
  • "Is it possible for both the functions to return the same value during runtime?" - So ehh No, they are unique :) – Ropstah Jun 02 '09 at 15:34
2

Whether they can have the same value depends on the JavaScript implementation. As Maciej mentioned in Firefox they can't have the same value as the same counter is used. However, that might be different in other browsers, so it is perhaps best not to rely on them never having the same value.

Mario Menger
  • 5,862
  • 2
  • 28
  • 31
0

It sounds to me that the returned value is the index value for whatever internally maintained list of timers/intervals they are keeping.

As in point, I called clearInterval(18) instead of clearInterval(var_returned_from_set) and it stopped the desired timer/interval. (Tested FF17.0.1 and IE9.0.8)

Also in my own testing they appear to be unique for the lifetime of the page for both of those browsers.

Plater
  • 105
  • 1
  • 5