Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a browser event loop?

Tags:

I have been doing some web application programming using GWT and have been confused by the term "browser event loop".

I have encountered situations where I need to execute deferred commands and "do something" after the browser event loop completes.

I would like to know as to what exactly it is and what happens during the event loop process and in which order?

like image 678
4 revs, 3 users 80% Avatar asked Mar 24 '11 21:03

4 revs, 3 users 80%


People also ask

Do browsers have an event loop?

Since “the event loop” is nothing but a programming pattern, V8 allows the ability to plug-in an external event loop implementation to work with its JavaScript runtime. Using this flexibility, the Chrome browser uses libevent as its event loop implementation, and NodeJS uses libuv to implement the event loop.

What is meant by event loop?

In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program.

What is an event loop and how does it work?

The event loop is a process that waits for the Call Stack to be clear before pushing callbacks from the Task Queue to the Call Stack. Once the Stack is clear, the event loop triggers and checks the Task Queue for available callbacks.

What are event loop used for?

JavaScript has a runtime model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model is quite different from models in other languages like C and Java.


1 Answers

A browser event loop is a thread started by the browser that is constantly scanning for and running different events, just like it sounds. As events occur they are put in the event queue and run in turn by the one event thread. Your javascript should not create its own loops waiting for it to complete or anything like that...it will block that one continuous event loop thread. Instead you would use something like setTimeout or setInterval and check for whatever conditions you are waiting for so the browser can do work while it 'waits'.

GWT is nice in that it can co-opt this process somewhat using the scheduler -- in your case where you want to run something after the event loop 'completes' you will probably want to use scheduleFinally or scheduleDeferred. It will inject a handler for a piece of code into the event queue so that it will run after all other code in the current execution context (current execution context == where ever you are in the current JavaScript object hierarchy with the window as the root object) is run but before the next event that is placed in the queue.

like image 120
Ichorus Avatar answered Sep 16 '22 18:09

Ichorus