wordpress做企业网站,自己做的商业网站在那里发布,自己开发网站要多少钱,如何创建网站后台关于事件循环机制#xff0c;可以参考这篇文章
node 环境中的微任务
这里面补充一个#xff0c;node 中微任务是有优先级的#xff0c;常见的微任务有#xff0c;其中 process.nextTick 的优先级最高#xff0c;会优先执行#xff0c;剩下的按照进入微任务队列的顺序可以参考这篇文章
node 环境中的微任务
这里面补充一个node 中微任务是有优先级的常见的微任务有其中 process.nextTick 的优先级最高会优先执行剩下的按照进入微任务队列的顺序一次执行
process.nextTick 此方法是 node 中将回调函数安排在当前执行栈的尾部在I/O操作之后、事件循环的阶段之前执行所以他的优先级最高promise 是 js 的原生对象他的 .then 和 .catch 中注册的回调函数会被作为微任务。注意 new promise 中的代码会当做同步任务执行只有 then catch 里面的才是微任务并且在此之前一定要 resolve 或者 reject 改变promise 的状态才能执行queueMicrotask node 中全局作用于中定义的函数它接受一个函数作为参数会在微任务队列中添加一个任务。
可以自己看一下下面代码的输出注意需要在 node 环境下运行
console.log(1)
setTimeout(() {console.log(2)new Promise((resolve) {console.log(3)resolve()}).then(() {console.log(4)})queueMicrotask(() {console.log(5)})process.nextTick(() {console.log(6)})console.log(7)}, 10);
process.nextTick(function () { console.log(8) })
new Promise((resolve) {console.log(9)resolve()
}).then(() {console.log(10)
})
queueMicrotask(() {console.log(11)
})
输出结果如下 浏览器环境中的微任务
浏览器环境中的微任务没有优先级的区别都是按照进入队列的顺序执行的浏览器中的微任务主要有
promisemutationObserver 当DOM结构发生变化时注册的回调函数会以微任务的形式执行。queueMicrotask 在 window 上的一个全局的方案想不到吧浏览器也有这个方法不要以为他是 node 里面才有的
复制下面代码到开发者工具中运行即可查看结果注意找一个有 a 标签的网站
console.log(1)
new Promise((resolve) {console.log(2)resolve()
}).then(() {console.log(3)
})
// 监控一个元素
const node document.querySelector(a)
const observer new MutationObserver(() {console.log(5)
})
observer.observe(node, {attributes: true
})
// 修改元素属性用于监控
node.style.color red
queueMicrotask(() {console.log(4)
})
结果如下可以发现这三个微任务是没有特殊的优先级的 加油一切都是纸老虎只要学会这点题目事件循环统统都拿下