这是我写的帧速率计数器。使用requestAnimationFrame
是多余的还是重要的?
重写此函数以不使用requestAnimationFrame
会产生类似的输出。
const now = performance.now.bind(performance);
const raf = window.requestAnimationFrame;
const log = console.log.bind(console);
const ONE_SECOND = 1000;
let count = 0;
let start = now();
const go =
raf.bind(window, ()=> {
if (now() - start >= ONE_SECOND) {
log('FPS: ' + count);
count = 0;
start = now();
return setTimeout(go, 0);
}
count++;
setTimeout(go, 0);
});
go(); // 61, 60, 59, 60, 59...
修改
根据评论,这是一个经过修改的实施:
const raf = window.requestAnimationFrame;
const log = console.log.bind(console);
const ONE_SECOND = 1000;
let count = 0;
let start = performance.now();
const go =
raf.bind(window, (now)=> {
if (now - start >= ONE_SECOND) {
log('FPS: ' + count);
count = 0;
start = now;
return go();
}
count++;
go();
});
go();