13130300
请看这段代码:
for(var i=0; i<10; i++){
var now = new Date();
while (new Date - now < 1000);
console.log("here");
setTimeout(function(){
console.log(i);
}, 5000);
}
setTimeout在这应该是异步函数吧!我使用了while来使每个循环延迟1s,按理当执行第一次循环执行了setTimeout,他会在后台静默执行,不会阻塞。但是console.log(i);都是在循环执行完成后才会输出。请问这是什么原因?谢谢!
另外我注意到循环结束后立刻输出了4个console.log(i);那应该是执行循环后setTimeout就已经在后台执行了? 输出如下:here here here here here here here here here here 10 10 10 10 10 10 10 10 10 10