Skip to content

Commit 649ecc5

Browse files
committed
closes #1864
1 parent fbd6c8a commit 649ecc5

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

2-ui/2-events/05-dispatch-events/article.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ Please note: the event must have the flag `cancelable: true`, otherwise the call
211211

212212
## Events-in-events are synchronous
213213

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.
215215

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.
217217

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:
219221

220-
For instance, here the nested `menu-open` event is processed synchronously, during the `onclick`:
221222

222223
```html run autorun
223224
<button id="menu">Menu (click me)</button>
@@ -226,25 +227,27 @@ For instance, here the nested `menu-open` event is processed synchronously, duri
226227
menu.onclick = function() {
227228
alert(1);
228229
229-
// alert("nested")
230230
menu.dispatchEvent(new CustomEvent("menu-open", {
231231
bubbles: true
232232
}));
233233
234234
alert(2);
235235
};
236236
237+
// triggers between 1 and 2
237238
document.addEventListener('menu-open', () => alert('nested'));
238239
</script>
239240
```
240241

241242
The output order is: 1 -> nested -> 2.
242243

243-
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.
244247

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.
246249

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`:
248251

249252
```html run
250253
<button id="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
253256
menu.onclick = function() {
254257
alert(1);
255258
256-
// alert(2)
257259
setTimeout(() => menu.dispatchEvent(new CustomEvent("menu-open", {
258260
bubbles: true
259261
})));

0 commit comments

Comments
 (0)