You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/2-events/05-dispatch-events/article.md
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -211,13 +211,14 @@ Please note: the event must have the flag `cancelable: true`, otherwise the call
211
211
212
212
## Events-in-events are synchronous
213
213
214
-
Usually events are processed asynchronously. That is: if the browser is processing `onclick` and in the process a new event occurs, then it waits until the`onclick` processing is finished.
214
+
Usually events are processed in a queue. That is: if the browser is processing `onclick` and a new event occurs, e.g. mouse moved, then it's handing is queued up, corresponding `mousemove` handlers will be called after`onclick` processing is finished.
215
215
216
-
The exception is when one event is initiated from within another one.
216
+
The notable exception is when one event is initiated from within another one, e.g. using `dispatchEvent`. Such events are processed immediately: the new event handlers are called, and then the current event handling is resumed.
217
217
218
-
Then the control jumps to the nested event handler, and after it goes back.
218
+
For instance, in the code below the `menu-open` event is triggered during the `onclick`.
219
+
220
+
It's processed immediately, without waiting for `onlick` handler to end:
219
221
220
-
For instance, here the nested `menu-open` event is processed synchronously, during the `onclick`:
221
222
222
223
```html run autorun
223
224
<buttonid="menu">Menu (click me)</button>
@@ -226,25 +227,27 @@ For instance, here the nested `menu-open` event is processed synchronously, duri
Please note that the nested event `menu-open` fully bubbles up and is handled on the `document`. The propagation and handling of the nested event must be fully finished before the processing gets back to the outer code (`onclick`).
244
+
Please note that the nested event `menu-open` is caught on the `document`. The propagation and handling of the nested event is finished before the processing gets back to the outer code (`onclick`).
245
+
246
+
That's not only about `dispatchEvent`, there are other cases. If an event handler calls methods that trigger to other events -- they are too processed synchronously, in a nested fasion.
244
247
245
-
That's not only about `dispatchEvent`, there are other cases. JavaScript in an event handler can call methods that lead to other events -- they are too processed synchronously.
248
+
Let's say we don't like it. We'd want `onclick` to be fully processed first, independantly from `menu-open` or any other nested events.
246
249
247
-
If we don't like it, we can either put the `dispatchEvent` (or other event-triggering call) at the end of `onclick` or, maybe better, wrap it in zero-delay `setTimeout`:
250
+
Then we can either put the `dispatchEvent` (or another event-triggering call) at the end of `onclick` or, maybe better, wrap it in the zero-delay `setTimeout`:
248
251
249
252
```html run
250
253
<buttonid="menu">Menu (click me)</button>
@@ -253,7 +256,6 @@ If we don't like it, we can either put the `dispatchEvent` (or other event-trigg
0 commit comments