相关疑难解决方法(0)

确切的显示时间:requestAnimationFrame的用法和时间线

我要实现的是检测屏幕上出现某些更改的准确时间(主要是使用Google Chrome浏览器)。例如,我使用显示项目$("xelement").show();或使用进行更改$("#xelement").text("sth new");,然后我想查看Performance.now()到底是什么,当更改以给定的屏幕重新绘制出现在用户的屏幕上时。因此,我完全可以接受任何解决方案-在下文中,我主要指的是requestAnimationFrame(rAF),因为这是应该帮助实现此功能的函数,只是似乎没有实现。见下文。

基本上,正如我所想象的,rAF应该在0-17毫秒内执行其中的所有操作(每当下一帧出现在我的标准60 Hz屏幕上)。此外,timestamp参数应提供此执行时间的值(并且该值基于与performance.now()相同的DOMHighResTimeStamp度量)。

现在,这是我为此进行的众多测试之一:https : //jsfiddle.net/gasparl/k5nx7zvh/31/

function item_display() {
    var before = performance.now();
    requestAnimationFrame(function(timest){
        var r_start = performance.now();
        var r_ts = timest;
        console.log("before:", before);
        console.log("RAF callback start:", r_start);
        console.log("RAF stamp:", r_ts);
        console.log("before vs. RAF callback start:", r_start - before);
        console.log("before vs. RAF stamp:", r_ts - before);
        console.log("")
    });
}
setInterval(item_display, Math.floor(Math.random() * (1000 - 500 + 1)) + 500);
Run Code Online (Sandbox Code Playgroud)

我在Chrome中看到的是:rAF内的函数总是在大约0到3毫秒内执行(从紧接其前的performance.now()开始),最奇怪的是,rAF时间戳与我得到的完全不同rAF内的performance.now()通常比在rAF 之前调用的performance.now()早大约0-17毫秒(但有时在之后0-1毫秒)。

这是一个典型的例子:

before: 409265.00000001397
RAF callback start: 409266.30000001758
RAF stamp: 409260.832 
before …
Run Code Online (Sandbox Code Playgroud)

javascript animation timing requestanimationframe display

5
推荐指数
1
解决办法
362
查看次数