我正在创建一个html5画布游戏,想检查在一秒钟内通过多少帧。我尝试使用新的Date(),但无法正常工作。我将如何做到这一点?javascript模板/示例:
var fps;
function loop(){
//some code
window.requestAnimationFrame(loop);
}
function checkFrameRate(){
fps = //how long it takes for loop to run
window.requestAnimationFrame(checkFrameRate);
}
loop();
checkFrameRate
在循环功能中,检查两次执行之间经过的时间。
let lastTime = new Date();
function loop() {
let currentTime = new Date();
// currentTime - lastTime is the number of milliseconds passed from last loop
var fps = 1000 / (currentTime - lastTime);
lastTime = currentTime;
}