JavaScript运行机制(二) 事件循环
<script>
process.nextTick(()=>{
console.log(1)
})
console.log(2);
setTimeout(()=>{console.log(3)},0);
console.log(4);
</script>
<script>
setImmediate(()=>{
console.log(1)
})
process.nextTick(()=>{
console.log(2)
})
console.log(3);
setTimeout(()=>{console.log(4)},0);
console.log(5);
</script>
setImmediate(()=>{
console.log(1)
})
process.nextTick(()=>{
console.log(2)
})
console.log(3);
setTimeout(()=>{console.log(4)},0);
setTimeout(()=>{console.log(5)},1000);
setTimeout(()=>{console.log(6)},0);
console.log(7);
1.
</script>
总结: 执行顺序 1.同步 2.process.nextTick() 3.异步 4.setImmediate(当前事件循环结束执行 )
|