HTML5規範中規定瀏覽器
If timeout is less than 0, then set timeout to 0.
If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.bash
也就是說,定時器在嵌套層級超過5層時,最小延遲變爲4ms,以下代碼,層級一直嵌套less
function cb() { f(); setTimeout(cb, 0); }
setTimeout(cb, 0);
複製代碼
setInterval(f, 0);
複製代碼
實際在瀏覽器的實現中,不一樣瀏覽器規定的嵌套層級和最小延遲都有所不一樣:在Chrome和Firefox中定時器的第5次調用被阻了,Safari是在第6次,Edge是在第3次;Chrome的Blink最小延遲是1msui
// https://chromium.googlesource.com/chromium/blink/+/master/Source/core/frame/DOMTimer.cpp
if (!(after >= 1 && after <= TIMEOUT_MAX))
after = 1; // schedule on next tick, follows browser behavior
複製代碼
在Node中也不同,沒有說嵌套層級,就是最小延遲是1msgoogle
When delay is larger than 2147483647 or less than 1, the delay will be set to 1.spa
歡迎關注個人微博@狂刀二code