如何解决如何使用JavaScript感知帧速率?
(我是JavaScript的新手。)我创建了(或至少尝试过)一个脚本,试图在程序运行时记录和显示设备的平均FPS,但我似乎无法获得FPS感应零件工作。 (他们可能是更简单的方法,也可能是更准确的方法,但我想看看我做错了什么地方或应该改进的地方。)
function getAverageFPS() {
let placeholder1 = 0;
let placeholder2 = 0;
function getFPS() {
/* checks the frame rate by dividing 1 by the seconds passed since
the script was last run. (The divide by 1000 is to convert Date.now()
into seconds.) */
placeholder1 = performance.now();
let frames = Math.round(1 / ((placeholder1 - placeholder2) / 1000));
placeholder2 = performance.now();
return frames;
}
/* the below is unneccessary,it just demonstrates use somewhat
in a program you would run the getFPS() function after the main
tick or script every time and this could be used to get the FPS. */
// check the FPS 10000 times and store it
let avFPS = [];
for (let i = 0; i < 10000; i += 1) {
avFPS.push(getFPS());
}
// get the average of the FPS
let average = 0;
for (let ii = 0; ii < avFPS.length; ii += 1) {
average += avFPS[ii];
}
average = average / avFPS.length
console.log(average);
}
(我知道在for
循环中我可以使用i++
,但是i += 1
对我来说更有意义,我更喜欢使用它。)
这将返回NaN,我正在尝试弄清楚如何使其工作。
编辑:
使用上面的注释和代码修复的是可行的解决方案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。