-
click
lock 5s
button: JavaScript enters a dead loop. Browser main thread blocked -
Within five seconds of being locked, enter any amount of text in the input or click the btn button any number of times.
-
Waiting for unlocking. Then you will find that the console suddenly prints a lot of text.
My expectation is that user actions will not be responded to during the blocking process. When the lock is released, the console only outputs start lock
and end lock
. But in fact, it’s not like that, so I’m very confused.
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<input id="input" type="text" />
<button id="lock">lock 5s</button>
<button id="btn">btn</button>
<script>
document.querySelector("#input").addEventListener("input", () => {
console.log("on input", input.value);
});
document.querySelector("#lock").addEventListener("click", () => {
console.log("start lock");
const start = performance.now();
while (performance.now() - start < 5000) continue;
console.log("end lock");
});
document.querySelector("#btn").addEventListener("click", () => {
console.log("btn");
});
</script>
</body>
</html>
2
Answers
As mentioned in the comments the events are queued.
An interesting case that shows that events are processed after the macrotask is complete.
When the lock is over we add a DIV that pushes the button down.
Then the queued events are processed against the updated DOM (without re-rendering).
Since there’s no button anymore under the points the mouse was clicked the mouse events goes into the body.
The seconds example make it more obvious that the events goes against the updated DOM, before any rendering:
Blocking the main JS thread is something you want to avoid..
If your just wanting to disable user interaction for 5 seconds, one idea is just place a div full screen over your page, you could have it set to transparent.
Alternatively you could even create a
<dialog/>
element informing the user, rather than locking up the UI. Doing this click events are still processed in the event loop, and then won’t stack as in your example.eg.