Who or what thread owns the Javascript event loop?

I read a lot of articles here about Javascript event loops. They mainly talk about how the event loop works with the web API or JS runtime, for example. moving an event from the web API to the event queue to be processed, etc. But I don’t see any of them explaining where the event loop is running, which really leaves me with a lot of questions about the event loop. For example, who triggers an event loop? Is the thread runtime loop the same as the JS runtime thread, which is single-threaded and executes your JS code? Who owns the event loop? Is the event loop fired there forever while the browser windows are open? I understand (not exactly :() the event loop fires when the main runtime code is complete,but how does this happen in the event loop? Can someone help answer my questions to help me understand the cycle of events? Thank!

+4
source share
1 answer

  who triggers the event loop? Who owns the event loop?

JavaScript Engine(e.g. V8 in Chrome) contains event loop. The event loop got its name because of how it is usually implemented, which usually resembles:

while(queue.waitForMessage()){
  queue.processNextMessage();
}

queue.waitForMessage synchronously awaits the arrival of a message if it is not currently available.

Is the thread start loop the same JS thread that is single-threaded and executes JS code?

JavaScript is that single-threaded, at run time, there is only one thread (the JavaScript engine) that contains event loop.

, ? , , , ?

, JavaScript. , , , JavaScript.

, , JavaScript Engine.

function init() {
  var link = document.getElementById("foo");

  link.addEventListener("click", function changeColor() {
    this.style.color = "burlywood";
  });
}

init();


(: carbonfive.com)

:

http://altitudelabs.com/blog/what-is-the-javascript-event-loop/

http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/

+4

Source: https://habr.com/ru/post/1629087/


All Articles