cswork / OA打开练习工作台

PaycomOA #18

Q18. JS Event Loop — setTimeout Order

估计简单概念题评测数据准备中,可先阅读题面

考点(AI 标注)模拟/实现

In what order will the numbers 1-4 be logged to the console when the code below is executed?

(function() {
 console.log(1);
 setTimeout(function(){ console.log(2); }, 1000);
 setTimeout(function(){ console.log(3); }, 0);
 console.log(4);
})();
  • 1 2 3 4 / 4 3 2 1 / 1 4 3 2 <- / 1 3 4 2

Sync log fires first (1, 4); both setTimeout callbacks go to the macrotask queue — the 0ms one fires before the 1000ms one (3, then 2).