在javascript/html中显示FPS

在javascript/html中显示FPS,javascript,html,Javascript,Html,我正在制作一个游戏,我正在尝试添加一个功能来向用户显示FPS。我一直在看stackoverflow和其他网站上的一些其他问题,但它们似乎对我不起作用。如果你能回答,我将不胜感激。谢谢 (已回答)以下解决方案适合您吗 // window.requestAnimFrame=(函数(){ return window.requestAnimationFrame|| window.webkitRequestAnimationFrame|| window.mozRequestAnimationFrame|

我正在制作一个游戏,我正在尝试添加一个功能来向用户显示FPS。我一直在看stackoverflow和其他网站上的一些其他问题,但它们似乎对我不起作用。如果你能回答,我将不胜感激。谢谢


(已回答)

以下解决方案适合您吗

//
window.requestAnimFrame=(函数(){
return window.requestAnimationFrame||
window.webkitRequestAnimationFrame||
window.mozRequestAnimationFrame||
window.iereRequestAnimationFrame||
函数(回调){
设置超时(回调,1000/60);
};
})();
让fpseelement=document.getElementById(“fps”);
让then=Date.now()/1000;//以秒为单位计算时间
让render=function(){
让now=Date.now()/1000;//以秒为单位获取时间
//计算自上一帧起的时间
让elapsedTime=now-then;
然后=现在;
//计算fps
设fps=1/elapsedTime;
fpsElement.innerText=fps.toFixed(2);
请求帧(渲染);
};
render()

fps:
到目前为止,您尝试了什么?为什么你认为其他的解决方案对你不起作用?我尝试过其他的堆栈溢出解决方案,但我不知道是我的电脑还是别的什么,但它不起作用。谢谢你的回答。