在Mozilla前端工程师性能最佳实践文档中,建议将
setTimeout
与requestAnimationFrame
结合使用,在重绘后立即运行某些内容: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);
});
为什么这是推荐的解决方案?是什么使得这成为在重新绘制后安排某事的最佳方式?