gpt4 book ai didi

javascript - JS浏览器事件处理

转载 作者:行者123 更新时间:2023-11-28 00:41:48 25 4
gpt4 key购买 nike

现在我对浏览器事件有点困惑。我仍然不确定事件循环如何开始工作。例如,当我在某个函数中间分派(dispatch)自定义事件时,事件处理是立即开始还是移至事件队列? 那么其他事件呢?如果我单击或加载页面,会发生什么?事件处理过程什么时候开始?

最佳答案

I'm still not sure how event loop starts working.

通常在浏览器中,它会在您进入页面时立即开始工作,并在您退出页面时停止工作。

For example, when I dispatch custom event in the mid of some function, does event handling start right away or is it moved to the event queue?

有同步事件(立即触发)和异步事件在下一次迭代中触发。除非您明确终止代码,否则您的代码通常会在任何其他运行之前运行到其执行结束。

DOM 自定义事件通常是同步的。也就是说 - 你是触发它们的人,并且它会立即发生。

例如,如果您正在运行代码并且发生了“click”事件,或者触发了使用setTimeout设置的计时器,您的代码将首先完成运行 - 没有“中断”你的代码。

When does the event handling process begin?

As specified :

Events may be dispatched either synchronously or asynchronously.

Events which are synchronous (sync events) must be treated as if they are in a virtual queue in a first-in-first-out model, ordered by sequence of temporal occurrence with respect to other events, to changes in the DOM, and to user interaction. Each event in this virtual queue must be delayed until the previous event has completed its propagation behavior, or been canceled. Some sync events are driven by a specific device or process, such as mouse button events. These events are governed by the event order algorithms defined for that set of events, and a user agent must dispatch these events in the defined order.

关于javascript - JS浏览器事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com