Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of execution of functions bound to an event in Javascript

I was searching for details regarding the order of execution of functions bound to a page event in javascript, for example via an EventListener.

For example, if I bind three functions A(), B() and C(), to the same event (say DOMContentLoaded), what will be the order of execution of these three functions? One-by-one based on the order of addEventListener call in JS code? Or they are executed all simultaneously?

Besides, can I modify this order? For example, to have a function, bound to DOMContentLoaded event listener, to be executed before any other function bound to the same event.

like image 602
Carmine Giangregorio Avatar asked Feb 12 '15 15:02

Carmine Giangregorio


1 Answers

Event handlers are always called in the order in which they were registered.

Once registered, you cannot insert additional handlers ahead of them[*].


[*] unless you are some how able to obtain a list of all the handlers, and their EventListener objects, and call removeEventListener to remove them, insert your own, and then reinsert the originals. In practise this is likely to be impossible.

like image 183
Alnitak Avatar answered Oct 13 '22 00:10

Alnitak