javascript - 为什么 requestIdleCallback 给我超过 16 毫秒的时间

标签 javascript google-chrome

我认为我们的代码执行时间大约为 16 毫秒,因为渲染速率为 60 fps,所以 1000/60 = ~16 毫秒。但是,如果我在 Chrome 中运行它:

requestIdleCallback((deadline)=>{
    console.log(deadline.timeRemaining(), deadline.didTimeout)
});

timeRemaining 给我 49.9ms 的时间。这是否意味着 Chrome 不会每 16 毫秒重新渲染一次?我假设在这种情况下是正确的,如果没有任何变化,则无需重新渲染。

最佳答案

规范支持您的假设。在 Start an idle period algorithm它说:

  1. Let deadline be a time in the future until which the browser expects to remain idle. The user agent should choose deadline to ensure that no time-critical tasks will be delayed even if a callback runs for the whole time period from now to deadline. As such, it should be set to the minimum of: the closest timeout in the list of active timers as set via setTimeout and setInterval; the scheduled runtime for pending animation callbacks posted via requestAnimationFrame; pending internal timeouts such as deadlines to start rendering the next frame, process audio or any other internal task the user agent deems important.

(我的重点)

因此,如果存在渲染下一帧的内部挂起超时,则截止日期必须在此之前。

关于javascript - 为什么 requestIdleCallback 给我超过 16 毫秒的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53059977/

相关文章:

google-chrome - Selenium 中的 Adob​​e Flash、Chrome 54.0.2840.59-1 : "This plugin is not supported"

Javascript 同源策略和 Google Chrome 控制台

javascript - 为什么 Angular 4 plunker 可在 Safari 上运行,但不能在 Chrome 上运行

javascript - FOR 循环遍历数组

javascript - jquery可以读取外部页面的dom元素吗?

javascript - 如何限制在 jQuery slider 上按下按键的频率?

javascript - Chrome : Why change style in batch(cssText) is slower then change in single(. style.property) 方式?

javascript - 未捕获的类型错误 : no method 'responsiveSlides'

javascript - 尝试使用 d3.time.format 格式化日期时出现 JavaScript 错误 "d.getMonth is not a function"

javascript - 如何在 Firefox 中添加自定义 HTML 元素? (Chrome 有效)