Why is there a difference in the task/microtask execution order when a button is programmatically clicked vs DOM clicked?

后端 未结 3 1707
走了就别回头了
走了就别回头了 2021-01-01 16:23

There\'s a difference in the execution order of the microtask/task queues when a button is clicked in the DOM, vs it being programatically clicked.

3条回答
  •  难免孤独
    2021-01-01 16:45

    dispatchEvent

    Unlike "native" events, which are fired by the DOM and invoke event handlers asynchronously via the event loop, dispatchEvent invokes event handlers synchronously. All applicable event handlers will execute and return before the code continues on after the call to dispatchEvent.

    dispatchEvent is the last step of the create-init-dispatch process, which is used for dispatching events into the implementation's event model. The event can be created using Event constructor.

提交回复
热议问题