Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Javascript event queue have priority?

These days, I have read some documents about setTimeout and setInterval. I have learned that the Javascript is a single thread which will only execute one piece of code per time. At the same time, if there is a event happens, it will be pushed into the event queue and block until appropriate time. I want to know, when many events are blocked waiting to execute at the same time. Do these events have different priorities, so the high priority event will execute before the low ones. Or just a FIFO queue.

setTimeout(fn1, 10);
$(document).click(fn2); //will be called at 6ms;
$.ajax({ajaxSuccess(fn3); //async request,it uses 7ms;})

 for () {
    //will run 18ms;
};

In the above code, the setTimeout fn1 will happen at 10 ms,click event handler fn2 will at 6ms, ajax callback fn3 will at 7ms, but all the three functions will be blocked until the for loop finish. At 18ms, the for loop finished, so what order does these functions will be invoked.(fn1,fn2,fn3) or (fn2,fn3,fn1)

like image 646
xff1874 Avatar asked Nov 02 '13 15:11

xff1874


People also ask

What is JavaScript event queue?

Queue. A JavaScript runtime uses a message queue, which is a list of messages to be processed. Each message has an associated function that gets called to handle the message. At some point during the event loop, the runtime starts handling the messages on the queue, starting with the oldest one.

Which queue gets priority from the event loop while pushing the callbacks to the call stack?

Microtask Queue has higher priority than Callback Queue of fetching the callback functions to Event Loop.

How JavaScript works event loop stack and queue?

The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, the Event Loop will take the first event from the queue and will push it to the Call Stack, which effectively runs it. Such an iteration is called a tick in the Event Loop.

What is the difference between the stack and the queue within the JavaScript event model?

Follow the path that the execution of setTimeout takes, and in another run, focus on what the call stack does. Unlike the call stack, the callback queue follows the FIFO order (First In, First Out), meaning that the calls are processed in the same order they've been added to the queue.


1 Answers

Does javascript event queue have priority?

Sort of. The event loop is actually composed of one or more event queues. In each queue, events are handled in a FIFO order.

It's up to the browser to decide how many queues to have and what form of prioritisation to give them. There's no Javascript interface to individual event queues or to send events to a particular queue.

https://www.w3.org/TR/2014/REC-html5-20141028/webappapis.html#event-loops

Each task is defined as coming from a specific task source. All the tasks from one particular task source and destined to a particular event loop (e.g. the callbacks generated by timers of a Document, the events fired for mouse movements over that Document, the tasks queued for the parser of that Document) must always be added to the same task queue, but tasks from different task sources may be placed in different task queues.

For example, a user agent could have one task queue for mouse and key events (the user interaction task source), and another for everything else. The user agent could then give keyboard and mouse events preference over other tasks three quarters of the time, keeping the interface responsive but not starving other task queues, and never processing events from any one task source out of order.

like image 193
r3m0t Avatar answered Sep 21 '22 00:09

r3m0t