浏览器事件循环


// 按顺序最后都放入 js stack

setTimeout 定时任务会被 requestAnimationFrame 影响
// 可能是整合了 setTimeout 和 requestAnimationFrame 异步任务

Summary:
  • Tasks execute in order, and the browser may render between them
  • Microtasks execute in order, and are executed:
    • after every callback, as long as no other JavaScript is mid-execution
    • at the end of each task

// 现在浏览器间有差别
Tasks:如 click handler, setTimeout, setInterval, requestIdleCallback, requestAnimationFrame

Microtasks(jobs): 如 async, promise.then, MutationObserver(放到微任务队列末尾,当前任务中执行

用户代理触发的事件放在微任务队列末尾, element.click() 的冒泡事件则在当前任务中(当前 script 任务中连续 click 触发,因为还在 js stack 中)(事件循环中清除 callback,类似 ECMAScript: Jobs and Job Queues

异步 IO ?