0

setTimeout(() => console.log('1'), 1);

setTimeout(() => console.log('4'), 0);

setTimeout(() => console.log('3'), 2);

setTimeout(() => console.log('2'), 0);

I expected the output to be 4,2,1,3 but the output is 1,4,2,3. How the first setTimeout with delay 1 is getting executed even before 0?

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
HSM
  • 27
  • 6
  • Oh ok.. I checked in chrome and opera. Didn't try it in firefox though. Thank you. – HSM Mar 15 '22 at 05:28
  • 1
    @HSM, this [SO question](https://stackoverflow.com/questions/8341803/what-is-the-difference-between-settimeoutfn-0-and-settimeoutfn-1?msclkid=6b3e3f57a42111ecb968177397d51647) might has [this answer](https://stackoverflow.com/a/37961816/1577058) – Frank Fajardo Mar 15 '22 at 05:35
  • Yes Chrome has a minimum timeout of 1ms currently, but they are working on removing that limitation: https://crbug.com/402694 Node also has the same, but not Firefox (though [they do have weirdnesses](https://bugzil.la/1270059) too where callbacks scheduled before the page load have less priority than others) – Kaiido Mar 15 '22 at 05:39
  • 1
    @Bravo no the other way around. Firefox did set this special task queue for timers before load, because they thought it's what Chrome was doing while they actually just had this minimum 1ms timeout. This was creating interop issues where some big players were facing different results when ran in Chrome or in Firefox. And regarding your first comment, the specs clearly state that the UA can add as many delay as they see fit for whatever reason they want. Your rant against Chrome is not funded and useless. – Kaiido Mar 15 '22 at 05:43
  • @Bravo https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#run-steps-after-a-timeout ¶5.3 "Optionally, wait a further implementation-defined length of time." That's not against specs. – Kaiido Mar 15 '22 at 05:48
  • @Bravo what are you talking about... Why would that have any relation with Google Docs? The point is they do this, and they are allowed to do this. Yes there are some things that they purposely do against the specs, but when they do they open a discussion about changing the specs to follow. Once again, your rants don't bring anything useful here. And you should probably familiarize yourself with how web-standards work if you wish to continue on this path of claiming "they are wrong!". – Kaiido Mar 15 '22 at 06:06
  • @Kaiido - chromium had an intent to remove the clamp January 2021 - one reason given is because the 1ms clamp does not follow the specs - so - petard, own, hoisted, your, by (that's how it comes out using setTimeout) - I find that when the clamp was added is more interesting reading – Bravo Mar 15 '22 at 06:26

2 Answers2

-2

Because the time you used is very less once the code is executed it shows as per the flow.

setTimeout(() => console.log('1'), 2000);

setTimeout(() => console.log('4'), 0);

setTimeout(() => console.log('3'), 3000);

setTimeout(() => console.log('2'), 0);
-2

It is because till, the line number 2 setTimeout(() => console.log('4'), 0); is loading/rendering and converting into binary, before rendering only, the 1 ms is completed, resulting in 1 ms setTimeout() working before 2 ms setTimeout()

Zayaan
  • 169
  • 1
  • 8