<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
let callbacks = [];
let pending = false;
for (var i = 0; i <4; i++) {
if(i == 1){
nextTick(function(){
console.log("更新1");
});
}else{
nextTick(function(){
console.log("更新2");
})
}
}
// 存储nextTick
function nextTick (cb) {
callbacks.push(cb);
if (!pending) {
// 代表等待状态的标志位
pending = true;
setTimeout(flushCallbacks, 0);
}
}
function flushCallbacks () {
console.log("---1---");
pending = false;
const copies = callbacks.slice(0);
callbacks.length = 0;
for (let i = 0; i < copies.length; i++) {
copies[i]();
}
}
</script>
</body>
</html>
来源:CSDN
作者:黑骑军
链接:https://blog.csdn.net/m0_37902100/article/details/104032754