Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout(): If not defined in EcmaScript spec, where can I learn how it 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?

like image 546
user967722 Avatar asked Jan 13 '12 14:01

user967722


People also ask

Why is setTimeout not working?

This is due to when a function is executed as a parameter to setTimeout , the execution context is different to the execution context of the function! Now this will print out undefined because the this keyword is executed in the context of the setTimeout function and is therefore not defined.

What is the use of setTimeout () in JavaScript?

setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

What is the alternative for setTimeout in JavaScript?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.

What will happen if we call setTimeout () with a time of 0 ms?

1 Answer. To explain: If you call setTimeout() with a time of 0 ms, the function you specify is not invoked right away. Instead, it is placed on a queue to be invoked “as soon as possible” after any currently pending event handlers finish running.


1 Answers

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

like image 98
T.J. Crowder Avatar answered Oct 15 '22 03:10

T.J. Crowder