关于JS的运行机制,虽然弄清了时间循环,但对于具体是什么时候渲染页面还很混乱,看了很多文章,接下来通过几个例子来演示
首先,js渲染页面是在微任务结束后渲染,
宏任务-->微任务-->渲染-->宏任务......
document.body.style = 'background:blue'
console.log(1);
Promise.resolve().then(()=>{
console.log(2);
document.body.style = 'background:red'
setTimeout(function(){
console.log(3);
document.body.style = 'background:pink'
},1000)
});
Promise.resolve().then(()=>{
console.log(4);
document.body.style = 'background:black'
setTimeout(function(){
console.log(5);
document.body.style = 'background:yellow'
},100)
});
console.log(6);
待补充......