简体   繁体   English

将回调直接排入事件队列

[英]Queue a callback directly into an event queue

在javascript中,有没有办法将回调函数直接排队到事件队列而不是setTimeout,并将0作为第二个参数?

In the browser you can use requestAnimationFrame or requestIdleCallback : 在浏览器中,您可以使用requestAnimationFramerequestIdleCallback

requestAnimationFrame(() => console.log('Browser is repainting'));

requestIdleCallback(() => console.log('Browser has run out of things to do'));

In Node.js you can use process.nextTick : 在Node.js中,您可以使用process.nextTick

process.nextTick(() => console.log('Next tick in the event loop'));

Note: requestIdleCallback is fairly new at the time of this answer, so check browser compatibility before using it in your application. 注意:在回答这个问题时, requestIdleCallback相当新,所以在您的应用程序中使用它之前, 请检查浏览器兼容性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM