なぜ0ではなくあえて1を指定するのでしょうか?
lodashやUnderscore.jsのdefer
でsetTimeout(func, 0)
ではなく1msが指定されている理由は、古いChromeでUIスレッドがブロックされることがあるからのようです(?)。
https://github.com/lodash/lodash/pull/1334#issuecomment-120736265
We avoid a setTimeout of 0 because I believe old Chrome could end up blocking the UI thread with them.
setTimeout(func, 0)
について
7.4. Timers
http://w3c.github.io/html/webappapis.html#timers
timeout
の初期値は0msで省略可能です。
ただし、タイマーのnesting level
が5より大きくtimeout
が4msより小さい場合、timeout
は4msに設定されます。
NOTE:
Timers can be nested; after five such nested timers, however, the interval is forced to be at least four milliseconds.
- If nesting level is greater than 5, and timeout is less than 4, then increase timeout to 4.
0ms(初期値)の場合、scheduled to run at the next opportunity
ですが、いつ実行されるかはタスクに積まれているキューの状態に依存します。
EXAMPLE 11
http://w3c.github.io/html/webappapis.html#example-8b53adfc
function rescheduleWork() {
var handle = setTimeout(rescheduleWork, 0); // preschedule next iteration
if (doExpensiveWork())
clearTimeout(handle); // clear the timeout if we don’t need it
}
https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout#Notes
Because even though setTimeout was called with a delay of zero, it's placed on a queue and scheduled to run at the next opportunity, not immediately.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Zero_delays
Zero delay doesn't actually mean the call back will fire-off after zero milliseconds. Calling setTimeout with a delay of 0 (zero) milliseconds doesn't execute the callback function after the given interval. The execution depends on the number of awaiting tasks in the queue.
(function () {
console.log('this is the start');
setTimeout(function cb() {
console.log('this is a msg from call back');
});
console.log('this is just a message');
setTimeout(function cb1() {
console.log('this is a msg from call back1');
}, 0);
console.log('this is the end');
})();
If nesting level is greater than 5, and timeout is less than 4, then increase timeout to 4.
です。timeout
の初期値は0ms(省略可)で、その場合は次のタイマータスク発火時に実行されます。(ただし実行順はタスクのキューの状態に依存) EventLoop#Zero_delays