21

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?

gofeddy
  • 579
  • 8
  • 20

1 Answers1

20

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.

Ichorus
  • 4,567
  • 6
  • 38
  • 46
  • 1
    Also, is the attachment of our widgets and UI elements to the DOM the last event in the loop, after which the browser event loop completes? – gofeddy Mar 25 '11 at 13:06
  • 1
    DOM manipulation events are handled in the exact same way as the other events. They are not necessarily the first or the last event to fire, just wherever they happen to land in the queue. They also bubble up the DOM tree so you can have listeners for those events anywhere on the tree 'above' where the event is triggered. The complicating factor, in vanilla js, is that each browser handles DOM mutation events differently. – Ichorus Mar 25 '11 at 14:13
  • GWT 'captures' events with the GWTEventSystem to try and create a unified event handling scheme. Events follow the tree bubbling scheme outlined above, but for each event triggered it first passed through the GWT HandlerManager which looks for Handlers registered for that type of event. If it finds one it triggers the EventHandler code if not it passes it back to the browser for standard handling. What are you trying to do, exactly? – Ichorus Mar 25 '11 at 14:34
  • Thanks for that reply. I was having a problem with doing some programmatic DOM manipulation using GWTP and was trying to figure out when and where exactly I would have to run the ScheduleDeferred() command. Here is the question I am talking about: http://stackoverflow.com/questions/5425195/gwt-implement-programmatic-tab-selection-of-a-tablayoutpanel-and-then-scroll-to – gofeddy Mar 25 '11 at 15:50
  • In general, switching tabs does not change the DOM. Both still exist and the tab switching is more or less a style trick. I am not 100% sure that is how GWT treats it. I will look into it and try to answer your other question. – Ichorus Mar 25 '11 at 21:53