在JavaScript中绑定到事件的函数的执行顺序 [英] Order of execution of functions bound to an event in Javascript

查看:121
本文介绍了在JavaScript中绑定到事件的函数的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索有关javascript中绑定到页面事件的函数的执行顺序的细节,例如通过EventListener。

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

例如,如果我绑定三个函数A(),B()和C(),到同一个事件(说DOMContentLoaded),这三个函数的执行顺序是什么?
一个一个根据addEventListener的顺序调用JS代码?或者他们同时执行?

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?

此外,我可以修改这个订单吗?例如,要绑定到DOMContentLoaded事件监听器的函数,在绑定到同一事件的任何其他函数之前执行。

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.

推荐答案

p>事件处理程序始终按照注册顺序调用。

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

注册后,您无法在其前插入其他处理程序[*]。

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

[*]除非你有一些能够获得所有处理程序的列表,而且他们的 EventListener 对象,并调用 removeEventListener 删除它们,插入自己的,然后重新插入原件。实际上这可能是不可能的。

[*] 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.

这篇关于在JavaScript中绑定到事件的函数的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆