4

Given the following code, when will function go be executed?

setTimeout(go, 0);

Will it be added to the end of the Job Queue immediately, or added to the end of the Job Queue after the minimum interval for setTimeout?

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • This post may help: http://stackoverflow.com/questions/779379/why-is-settimeoutfn-0-sometimes-useful – Brian Aug 12 '15 at 19:35
  • I thing in the next event loop. – webduvet Aug 12 '15 at 19:35
  • It likely depends on browser implementation. In chrome, it seems to be added after exactly 2ms. I couldn't get consistent results in firefox, sometimes it ran as fast as 1.25ms, throwing 2ms out the window. http://jsfiddle.net/q2tf393g/ – Kevin B Aug 12 '15 at 20:03
  • The documentation says 4ms, but based on the above test, i don't think it is accurate. – Kevin B Aug 12 '15 at 20:11

2 Answers2

2

Most browsers implement a minimum delay time of 4ms. You can use lower values without error, but when actually executing the script, the browser will overwrite the timeout value.

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout?redirectlocale=en-US&redirectslug=DOM%2Fwindow.setTimeout#Minimum.2F_maximum_delay_and_timeout_nesting

https://html.spec.whatwg.org/multipage/webappapis.html#timers

Ally Rippley
  • 525
  • 2
  • 6
  • Another good explanation can be found [here](http://geekabyte.blogspot.co.uk/2014/01/javascript-effect-of-setting-settimeout.html) – Matijs Aug 12 '15 at 21:34
0

with a timeout of 0, it will be added to the end of the job queue immediately

michael truong
  • 321
  • 1
  • 4