简体   繁体   English

浏览器中的事件循环和任务队列之间有区别吗?

[英]Is there a difference between event loop and task queue in browsers?

I am a little confused about some terms in the answer of this question: What is the event precedence in JavaScript?我对这个问题的答案中的一些术语感到有些困惑: JavaScript 中的事件优先级是什么?

Is there a difference between event loop and task queue and how big are can these queues?事件循环和任务队列之间有区别吗?这些队列有多大?

Because when I have set an Interval with setInterval() and interrupt this with an alert() then the intervals are dropped for the time where the alert is showing up.因为当我用setInterval()设置了一个间隔并用一个alert()中断它时,间隔会在警报出现的时候被删除。

Heap : Stores All variables , Objects , Functions and To all this memory is allocated:存储所有变量、对象、函数并分配给所有这些内存

Event Queue : He is the person contains list functions TOBE EXCECUTED By Stack .事件队列:他是包含列表函数TOBE EXCECUTED By Stack 的人

Stack : He is the main person who EXECUTES FUNCTIONS held by Event queue Stack :他是Event queue持有的EXECUTES FUNCTIONS的主要人物

Event Loop :事件循环

  1. He is the person(manager) who is in contact with Event Queue And Stack .他是与Event Queue And Stack接触的人(经理)。

  2. What he does Is .他所做的是。 Ifff The stack is empty and Event Queue Contains Functions to execute. Ifff堆栈事件队列包含要执行的函数 then push the first function from Event Queue to stack然后将第一个函数从事件队列推送到堆栈

visual Example 1 : latentflip视觉示例 1latentflip

This is an implementation detail - the specification is saying that an event loop can be use multiple task queues to store events.这是一个实现细节——规范说事件循环可以使用多个任务队列来存储事件。 Presumably there is no practical limit to the size of the queues.据推测,队列的大小没有实际限制。

For example, mouse/keyboard events could go into a special INPUT task queue that has a higher priority than other tasks, perhaps to make the UI more reponsive.例如,鼠标/键盘事件可以进入一个特殊的 INPUT 任务队列,该队列的优先级高于其他任务,也许是为了使 UI 更具响应性。

alert will interrupt the processing of events because it is a synchronous operation. alert会中断事件的处理,因为它是一个同步操作。 Presumably any applicable events would be queued in the meantime.据推测,任何适用的事件都将在此期间排队。

I think you're just seeing a protection mechanism in setInterval() .我认为您只是在setInterval()看到了一种保护机制。 If setInterval() isn't able to keep up with the desired interval rate, then it will skip intervals because if it didn't, then extra intervals might build up forever and that's not good as it would saturate a queue somewhere.如果setInterval()无法跟上所需的间隔率,那么它将跳过间隔,因为如果没有,那么额外的间隔可能会永远累积,这并不好,因为它会使某个队列饱和。

From everything I've seen in the queueing behavior, intervals and events go in the same queue and are processed in the order they were meant to occur.从我在排队行为中看到的一切来看,间隔和事件都在同一个队列中,并按照它们发生的顺序进行处理。 The difference is that if there is already a setInterval() callback in the queue that has not yet been processed, it won't put another one in the queue (thus skipping it).不同的是,如果队列中已经有一个尚未处理的setInterval()回调,它不会将另一个回调放入队列中(从而跳过它)。

Mousemove events are also processed specially so you can't fill the queue up with them either. Mousemove 事件也经过特殊处理,因此您也无法将它们填满队列。

The question you have posted is really a popular one, but you were not able to put it with clarity.您发布的问题确实很受欢迎,但您无法说清楚。

first things first js is synchronous by nature.首先js本质上是同步的。 The browser helps make it kind of asynchronous.浏览器有助于使其具有某种异步性。

The call stack of js works synchronously but whenever it encounters asynchronous piece of code (setTimeout(),HTTP requests) it is sent to the browser's web api's. js 的调用堆栈是同步工作的,但是每当它遇到异步代码段(setTimeout()、HTTP 请求)时,它就会被发送到浏览器的 web api。

where it is taken care of, like waiting for the response of the request or waiting for the timeout to complete.它被处理的地方,比如等待请求的响应或等待超时完成。 (The async piece of code is removed from the call stack for the time being, and call stack moves onto the next task) (异步代码段暂时从调用栈中移除,调用栈移至下一个任务)

After the web api recieves the response or the timeout fn ends it is then sent to the task queue which queues all the async code在 web api 收到响应或超时 fn 结束后,它会被发送到队列所有异步代码的任务队列

It is where the event loop actually comes into play.这是事件循环真正发挥作用的地方。 It first check's whether the call stack is empty if it is then the first element of task queue is pushed onto the stack.它首先检查调用堆栈是否为空,如果是,则将任务队列的第一个元素压入堆栈。

So in your case when you alerted ,it blocked the call stack and hence setInterval couldnt be pushed onto the call stack因此,在您发出警报的情况下,它阻止了调用堆栈,因此无法将 setInterval 推送到调用堆栈上

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

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