Was this helpful?
Registering callbacks in the Event Table
When the following code is executed,
the browser registers the two event handlers associated with the 'click' and 'keydown' events respectively in the event table. When an event occurs, for example, the user presses a key in the input box, the browser looks up the event table if there is any associated callback registered for that event. If it finds one, it pushes the callback function (alert in our example) to the queue where it waits for its turn to get executed.
An event queue is a staging area that enqueues messages and associated callbacks waiting to be processed and the event loop continually monitors the queue for messages. When the call stack is empty, the first callback in the queue is moved to the call stack for execution, and so on.
Monitoring the queue for callbacks in a loop
The event queue is implemented as a FIFO data structure, meaning that callbacks execute in the order they are added onto the queue. Note that the functions held in the event queue won't get executed until all of the functions in the call stack are executed no matter how time consuming they are. Let us discuss this in a little detail.
Note that when a function is grabbed off of the queue and placed in the call stack, that function is executed/processed completely until it reaches its synchronous completion. In this way, you can safely assume that a function will always execute to completion, without interruption, before any other code is executed.
The event messages are placed in the event queue in response to events, such as timeout, mouse being clicked, etc. given a callback function has been provided. When an event is fired, the associated callback is called with the event as a parameter. But if an event occurs and no associated callback function is provided, no message will be placed in the queue.
|posted||Sep 12, 2016|
|active||Sep 12, 2016|
|•||Reflection in .NET - Part 1|
|•||Updating a CSS class property in real-time|
|•||Client-side MVC pattern|
|•||Serialization in .NET - Part 4|
|•||Serialization in .NET - Part 3|
|•||Serialization in .NET - Part 2|
|•||Serialization in .NET - Part 1|
|•||How to create pretty URLs in Angular|
|•||In Angular what is the difference between ngRoute and ui-router|