如何使用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 (将#修改为@)