0

In HTML5 specs it talks about event loop which is responsible for processing events from the task queues. I was wondering which thread can enqueue events? Lets say if there is a UI event will there be a separate thread that will enqueue event and that event will be processed by the event loop? Can you give some reference from HTML5 specs to that question

ekhan
  • 231
  • 1
  • 4
  • 11
  • The internals of the browser are not written in Javascript and thus don't run on the Javascript thread. So, when a mouse click event happens in the browser window, the native browser code receives that event from the OS, it figures out what DOM element in the browser window is targeted and inserts the click event into the Javascript event queue. If no Javascript is currently running, the Javascript engine will execute that event right then. If some Javascript is currently running, then the event will remain in the queue until the current thread of Javascript finishes. – jfriend00 Jun 11 '15 at 23:43
  • You can read more about the JS event queue here [How does JavaScript handle AJAX responses in the background?](http://stackoverflow.com/questions/7575589/how-does-javascript-handle-ajax-responses-in-the-background/7575649#7575649) and [javascript internals: how events are implemented?](http://stackoverflow.com/questions/7972301/javascript-internals-how-events-are-implemented/7972367#7972367). – jfriend00 Jun 11 '15 at 23:45
  • In HTML5 UI events are not the only one generated. There could be other events like network response or timer events and All of those events are inserted into their respective queues. My question is who is responsible for inserting those events in the queue? Will every specific task generator be a separate thread? – ekhan Jun 12 '15 at 00:29
  • How the events get into the event queue is entirely implementation specific and will not be specified in any specification. Some browsers may use native threads, some may use an event driven messaging system from the OS. As far as the Javascript executing in the browser, which it is does not matter. The browser native code is responsible for inserting the events into the event queue. – jfriend00 Jun 12 '15 at 00:35

0 Answers0