最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout(): If not defined in EcmaScript spec, where can I learn how it works? - Stack Overflow

programmeradmin3浏览0评论

I had another question in SO about setTimeout(), where a user mentioned that if the function argument is a string it gets evaluated in global scope, other wise it's not. This was an eye-opener, so I tried to find more info about how setTimeout actually works, but it's not part of the EcmaScript spec and not even MDN had that specific of of information I found in SO.

Is there some good reference about how setTimeout() works?

I had another question in SO about setTimeout(), where a user mentioned that if the function argument is a string it gets evaluated in global scope, other wise it's not. This was an eye-opener, so I tried to find more info about how setTimeout actually works, but it's not part of the EcmaScript spec and not even MDN had that specific of of information I found in SO.

Is there some good reference about how setTimeout() works?

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Jan 13, 2012 at 14:41 user967722user967722 1,2291 gold badge20 silver badges27 bronze badges 5
  • 2 Here is the MDN page. – Pointy Commented Jan 13, 2012 at 14:43
  • 1 In the HTML5 draft: dev.w3.org/html5/spec/Overview.html#timers – James Allardice Commented Jan 13, 2012 at 14:45
  • the MDN does have some useful info about it: "Code executed by setTimeout() is run in a separate execution context to the function from which it was called." – Matt K Commented Jan 13, 2012 at 14:50
  • 1 @MattK: Yes, but the rest of that paragraph talks about this, which is different from scope. – T.J. Crowder Commented Jan 13, 2012 at 14:55
  • So if setTimeout and other similar API is not defined in ECMAscript, javascript itself should be synchronous language, right? can we say that? – Chris Bao Commented Mar 21, 2020 at 1:58
Add a comment  | 

2 Answers 2

Reset to default 16

setTimeout and such aren't in the ECMAScript specification because they're not JavaScript features. They're features of the browser environment's window object. Other environments (Windows Scripting Host, NodeJS, etc.) won't necessarily have those features.

The W3C has been trying to standardize the window object and its various features (including setTimeout), the latest is in the timers section of the HTML5 spec. A lot of it is codifying what browsers already do, although some of it (like saying that the minimum interval value must be 4 [milliseconds]) seems (to me) to be out-of-place for an API specification and implementations seem to make up their own minds (in tests, you can see current browsers happily doing a shorter interval, with the apparent exception of Opera which appears to do what the spec says).

The setTimeout() method is a method on the window object. You can find the link to the MDN documentation below:

https://developer.mozilla.org/en/DOM/window.setTimeout

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论