Skip to content

Commit d5f04d2

Browse files
committed
fixes
1 parent c14f447 commit d5f04d2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,20 @@ Please note that the nested event `menu-open` bubbles up and is handled on the `
241241

242242
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.
243243

244-
If we don't like it, we can either put the `dispatchEvent` (or other event-triggering call) at the end of `onclick` or, if inconvenient, wrap it in `setTimeout(...,0)`:
244+
If we don't like it, we can either put the `dispatchEvent` (or other event-triggering call) at the end of `onclick` or wrap it in zero-delay `setTimeout`:
245245

246246
```html run
247247
<button id="menu">Menu (click me)</button>
248248

249249
<script>
250-
// 1 -> 2 -> nested
250+
// Now the result is: 1 -> 2 -> nested
251251
menu.onclick = function() {
252252
alert(1);
253253
254254
// alert(2)
255255
setTimeout(() => menu.dispatchEvent(new CustomEvent("menu-open", {
256256
bubbles: true
257-
})), 0);
257+
})));
258258
259259
alert(2);
260260
};
@@ -263,6 +263,8 @@ If we don't like it, we can either put the `dispatchEvent` (or other event-trigg
263263
</script>
264264
```
265265

266+
Now `dispatchEvent` runs asynchronously after the current code execution is finished, including `mouse.onclick`, so event handlers are totally separate.
267+
266268
## Summary
267269

268270
To generate an event, we first need to create an event object.

0 commit comments

Comments
 (0)