Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript internals: how events are implemented?

My question is related to how the JS engines implement the pattern of asynchronous events when we do something like bind event handlers on a dom for lets say a click event?

Do they have something like a separate thread that is listening to all the click events? When an event does occur, do they refer the bind list and bubble up the events?

Similar is with Ajax,the asynchronous network call, where the browser spans a new thread that would start listening to the data from the server and when the response is received, it would call the success handler?

like image 334
sbr Avatar asked Nov 01 '11 20:11

sbr


People also ask

How do events work in JavaScript?

JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.

How are event handlers implemented?

An event handler is an implementation of the EventHandler interface. The handle() method of this interface provides the code that is executed when the event that is associated with the handler is received by the node that registered the handler. To register a handler, use the addEventHandler() method.

How do events work in browser?

Events are signals fired inside the browser window that notify of changes in the browser or operating system environment. Programmers can create event handler code that will run when an event fires, allowing web pages to respond appropriately to change.


1 Answers

Read this post about the javascript event queue and see if it answers most of your question. There will be a native OS thread that handles interfacing with actual OS events (mouse events, keyboard events, timer events, network I/O events, etc...) and those are then fed into the JS queue where the JS engine can further dispatch them to Javascript code. How many separate threads there are at the OS level is implementation specific and likely does vary with the implementation.

like image 132
jfriend00 Avatar answered Oct 06 '22 00:10

jfriend00