setTimeout
will wait for a time to pass, then it will execute the callback.
(setTimeout
将等待一段时间,然后将执行回调。)
requestIdleCallback
will wait for the main thread to be idle before executing the callback.
(requestIdleCallback
将在执行回调之前等待主线程空闲。)
Meaning that it won't slow animations, user can still click buttons and type into inputs.(这意味着它不会减慢动画的速度,用户仍然可以单击按钮并输入内容。)
How to choose between setTimeout and requestIdleCallback?
(如何在setTimeout和requestIdleCallback之间进行选择?)
setTimeout is best used when the callback must execute (Example: Sending data to an analytics server or logging out the user).(setTimeout最适合在必须执行回调的情况下使用(例如:将数据发送到分析服务器或注销用户)。)
requestIdleCallback is best for when you don't want to hurt the user experience in the cost of executing the callback (Example: Rerendering the DOM).(requestIdleCallback最适合您不希望在执行回调的成本上损害用户体验的情况下(例如:重新渲染DOM)。)
Keep in mind that requestIdleCallback is still experimental.
(请记住,requestIdleCallback仍处于试验阶段。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…