As the title says, how can I maintain/control the order of execution of functions in Jquery? I know we can establish many event handler via addEventListener
, but th
"I know we can establish many event handler via
addEventListener
, but the order of their execution isn't guaranteed."
Order of execution isn't guaranteed for event handlers bound with addEventListener()
, but order of execution definitely is guaranteed for event handlers bound with jQuery. jQuery calls your handlers in the same order they were bound.
This isn't made very obvious in giant bold text in the jQuery documentation, but it is there if you look hard enough. From the .on() method doco:
(Event handlers bound to an element are called in the same order that they were bound.)
Or in more detail from the .bind() method
When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.