Event loop in the browser generally includes these steps, repeatedly:
- executing script (task)
- executing microtasks
- render updates (if the browser is ready to present the next frame)
Meanwhile in our code, reading properties like el.offsetHeight
(full list here) could cause the browser to recalculate styles and perform layout synchronously, in order to return the up-to-date value. Does this change the event loop? For example, given this script block:
el.style.width = "200px"; // invalidate the style
el.offsetHeight; // forced synchronous layout
foo();
console.log("script end");
Usually, the whole block would be a task, the browser executes it to the last line, processes all the microtasks, then render updates if necessary. But with the second line triggering synchronous layout, how would the event loop be like? Does it pause executing, jump to step 3), render the updates, and continue with the rest of the code in the next tick, or does it just perform a layout and still be in step 1)?