javascripttimingv8internal-representation

How are timeouts represented in JavaScript?


When I call set timeout it returns a number, for example

> setTimeout(function(){ console.log('done'); },1000);
4431
"done"

I know that I can use this number to call clearTimeout() but what does this number represent internally?


Solution

  • It doesn't matter what it represents. It could be an address in memory, or an index into an array, or the National ID # of the programmer who wrote the interpreter + offset.

    You don't care. You better not care. Whatever it corresponds to today, internally on one particular browser / JS environment, it could well mean something else tomorrow somewhere else.

    All you need to know is that this number corresponds to your timer. If you need to do something to your timer later on, you'll want that number, so keep it safe. When the timer is dead, throw it away and never think of it again.


    If you're interested in some slightly-gritty details, see the spec for the list of active timeouts - there's no requirement that the number (the handle) represents anything internally, merely that it can be used to reliably identify a specific timer.

    If you're interested in some really gritty details, download the source for your favorite browser and go to town...