Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

浏览器执行机制的疑惑?

如有以下代码:假如页面上有一个DOM元素box


setTimeout(() => {
  console.log('a')  
})

//执行1000Ms同步任务

box.style.backgroundColor = 'red'

console.log(2)

想知道一下,这种是浏览器先渲染box的颜色还是先打印2。为什么?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

修改背景色,
输出 2,
输出 a

微任务在前,宏任务在后

宏任务: script, setTimeout, setInterval, setImmediate, I/O, UI rendering;
微任务: process.nextTick, Promise, Object.observe, MutationObserver;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...