logoalt Hacker News

jayfluxyesterday at 6:53 PM2 repliesview on HN

> Even if you’ve been doing JavaScript for a while, you might be surprised to learn that setTimeout(0) is not really setTimeout(0). Instead, it could run 4 milliseconds later:

Is this still the case? Even with this change? https://chromestatus.com/feature/4889002157015040


Replies

swsieberyesterday at 7:02 PM

I think it's still the case. The 4ms happens if you call setTimeout nested several times. I don't know the exact limit. But it's 5-ish times where that kicks in IIRC.

Edit: Here's the MDN bit on that, I was correct:

https://developer.mozilla.org/en-US/docs/Web/API/Window/setT...

> browsers will enforce a minimum timeout of 4 milliseconds once a nested call to setTimeout has been scheduled 5 times.

And the link from there to the spec about that:

https://html.spec.whatwg.org/multipage/timers-and-user-promp...

> If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.

timhhyesterday at 7:17 PM

I think that change is talking about the minimum timeout for the first 5 nested calls to `setTimeout(0)`.

Previously the first 5 would take 1ms, and then the rest would take 4ms. After that change the first 5 take 0ms and the rest take 4ms.