3

Can a number returned by setTimeout() in javaScript be negative?

Currently, I can see that the timeoutId's are 1,2,3,4,5,6.... in Chrome. In Firefox it starts from number 4 and so on.

Is this consistent across browsers and other js engines?

What if the timeoutId number will reach the maximum integer value?

ahsan ayub
  • 286
  • 2
  • 17
Midnight Guest
  • 1,863
  • 3
  • 16
  • 26
  • 2
    _“The returned `timeoutID` is a **positive integer** value which identifies the timer created by the call to `setTimeout()`”_ — [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout). – Sebastian Simon Sep 05 '18 at 07:48
  • 1
    possible duplicate https://stackoverflow.com/questions/940120/setinterval-settimeout-return-value – Joliver Sep 05 '18 at 07:49
  • 1
    [here](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout) you can check all the answer! – Margon Sep 05 '18 at 07:49
  • 1
    FWIW, Node.js ("other js engines") returns an object, not a number. – robertklep Sep 05 '18 at 07:52
  • 1
    _“let `handle` be a **user-agent-defined integer that is greater than zero**”_ — [the HTML WHATWG specification](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout). It’s not that surprising that NodeJS uses a different return value, since `setTimeout` and related functions are all specified in the HTML spec, not in ECMAScript. There’s [some discussion](https://esdiscuss.org/) for ES to have their own timer functions. – Sebastian Simon Sep 05 '18 at 07:59
  • The second question could be tried out with `while(Number.isSafeInteger(setTimeout(() => {}, 0)));`, and then, if the browser isn’t dead yet, try another `setTimeout` invocation. I certainly _wouldn’t_ try that… – Sebastian Simon Sep 05 '18 at 08:05

1 Answers1

5

According to Mozilla

The returned timeoutID is a positive integer value

It uses a "pool" of ID's. They may or many not be released back to the pool (it's down to browser implementation - comments suggest they're probably not). To use them all up, you're going to have to do a lot of work and it's almost certainly a programming error.

Note that there is nothing in there to detail how those ID's are generated exactly, so it's not necessarily guaranteed that different browsers will start from the same number. The pool is also shared by setInterval which will effect the ID's.

Ian
  • 33,605
  • 26
  • 118
  • 198
  • I don't think they'll ever reuse the same id in the same session no. Just imagine: I store a timeout id `1` in a variable. Then in an other event, like a click, I decide to clearTimeout this `1` id. If the browser did reuse this id to an other timeout, I would be clearing that new timeout instead of the first one I intended to kill if still alive. – Kaiido Sep 05 '18 at 08:05
  • Yes, the IDs are not released “back to the pool”. Just try two consecutive `setTimeout(() => {}, 0)` calls in the console. – Sebastian Simon Sep 05 '18 at 08:24
  • For a spec answer, 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. – Bob Jan 22 '19 at 03:11