JS 浏览器事件处理

JS browser event handling

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

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 自定义事件通常是同步的。也就是说 - 你是触发它们的人,它会立即发生。

例如,如果您有代码 运行ning 并且发生 "click" 事件或使用 setTimeout 设置的计时器被触发,您的代码将首先完成 运行ning -没有什么会“中断”您的代码。

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.