简体   繁体   English

DOM 事件在事件队列中的优先级

[英]DOM events priority in the event queue

Suppose I have this code:假设我有这段代码:

$('button').click(function onClick() {
    $('#divResult').text(Math.rand());
});

setInterval(function timeout() {
    console.log("Hello");
}, 300);

setInterval(function timeout() {
     console.log("Hi");
}, 200);

setInterval(function timeout() {
    console.log("Yo!");
}, 100);

So basically, after 300ms, I'll have more than 1 callback function in the event queue.所以基本上,在 300 毫秒之后,我将在事件队列中有不止 1 个回调函数。 Now, let's say that after some period, a user clicks on something.现在,假设在一段时间后,用户点击了某些东西。 This click event gets processed and the callback function onClick() goes inside the event queue too.此点击事件得到处理,回调函数 onClick() 也进入事件队列。 Let's say the first time it goes there, there are already 2 callback functions created by setInterval.假设它第一次到达那里时,setInterval 已经创建了 2 个回调函数。 Since this is a DOM-related function which will do re-rendering of the window, will it have priority over these functions?由于这是一个与 DOM 相关的函数,它将重新渲染窗口,它会优先于这些函数吗?

In this talk on event loops , the author mentions a render queue which is given a priority over the callback queue (where callbacks by methods like setTimeout and setInterval go).这个关于事件循环的演讲中,作者提到了一个渲染队列,它的优先级高于回调队列(通过setTimeoutsetInterval等方法进行的回调)。 Since onClick() does things related to rendering, will it go into this queue or into the regular callback queue and wait for its turn?既然onClick()做的是渲染相关的事情,那么它是进入这个队列还是进入常规的回调队列等待轮到呢?

Well I think I'll give it a shot, but this info is pretty new to me as well.好吧,我想我会试一试,但这个信息对我来说也是很新的。

Since this is a DOM-related function which will do re-rendering of the window, will it have priority over these functions?由于这是一个与 DOM 相关的函数,它将重新渲染窗口,它会优先于这些函数吗?

No, I don't believe that function itself will have any more priority than the functions inside of those intervals.不,我不相信函数本身会比那些区间内的函数有更高的优先级。 I don't think that the browser itself will know that the click callback will actually change the DOM.我不认为浏览器本身会知道点击回调实际上会改变 DOM。 After the callback function runs and the element's text changes, then the browser will repaint that node when it has a chance.在回调函数运行并且元素的文本发生更改后,浏览器将在有机会时重新绘制该节点。

Since onClick() does things related to rendering, will it go into this queue or into the regular callback queue and wait for its turn?既然onClick()做的是渲染相关的事情,那么它是进入这个队列还是进入常规的回调队列等待轮到呢?

I don't believe the onclick handler has to do anything related to rendering.我不相信onclick处理程序必须做任何与渲染相关的事情。 Yes, most of the time we code it to, but I don't think it always has to change an element's appearance.是的,大多数时候我们将它编码为,但我认为它并不总是必须改变元素的外观。 It could just send an Ajax request in the background.它可以在后台发送 Ajax 请求。 It could just log something to the console.它可以将某些内容记录到控制台。 You never really know.你永远不知道。

Also keep in mind that setInterval doesn't add an event onto the event queue to execute at a certain time later.还要记住, setInterval不会将事件添加到事件队列中以在稍后的特定时间执行。 It waits a certain time and then adds that event onto the event queue.它等待一定时间,然后将该事件添加到事件队列中。 You can read more on that here .您可以在此处阅读更多相关信息。

Hopefully I didn't make a fool of myself since I really didn't start diving deep into JS until a few months ago.希望我没有自欺欺人,因为直到几个月前我才真正开始深入研究 JS。 If I'm off about anything let me know.如果我有事请告诉我。

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

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