存在一个最小的时钟间隔
有关零秒延迟,此回调将会放到一个能立即执行的时段进行触发。JavaScript 代码大体上是自顶向下执行,但中间穿插着有关DOM渲染、事件回应等异步代码,它们将组成一个队列,零秒延迟将会实现插队操作
function sleep(milliSeconds) { var startTime = new Date().getTime() while (new Date().getTime() < startTime + milliSeconds) {} } var i = 0 setInterval(function() { console.log(+new Date(), ++i) sleep(4000) }, 2000) /* 间隔4s左右 1395043279715 1 1395043283750 2 1395043287753 3 1395043291757 4 1395043295763 5 1395043299763 6 */ /*setTimeout(function() { console.log(+new Date(), ++i) sleep(4000) setTimeout(arguments.callee, 2000) }, 2000) */ /* 间隔6s左右 1395042762150 1 1395042768158 2 1395042774169 3 1395042780177 4 1395042786177 5 1395042792177 6 */