In the mozilla docs on performance best practices for front-end engineers it's recommended to combine setTimeout
with requestAnimationFrame
to run something immediately after a repaint:
requestAnimationFrame(() => {
setTimeout(() => {
// This code will be run ASAP after Style and Layout information have
// been calculated and the paint has occurred. Unless something else
// has dirtied the DOM very early, querying for style and layout information
// here should be cheap.
}, 0);
});
Why is this the recommended solution? What exactly makes this the optimal way to schedule something right after a repaint?