开发者

How are timeouts represented in JavaScript?

开发者 https://www.devze.com 2023-03-15 03:45 出处:网络
When I call set timeout it returns a number, for example > setTimeout(function(){ console.log(\'done\'); },1000);

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 开发者_Go百科this number to call clearTimeout() but what does this number represent internally?


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...


I think it depends on the JS engine used, hence on the browser used. I know from older questions that each browsers uses a different way of enumerating the returned IDs.


It is the id representing the way to track the the event.


I think the usual way to do this is to set a variable equal to the setTimeout statement, such as myTO = setTimeout(...) then you clear it with clearTimeout(myTO).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号