第一种(基础版)
let lastTime = new Date()
let sampling = [], lastSample = ''
step()
function step(){
let curTime = new Date()
// console.log('step...')
let fps = curTime-lastTime>0?1000/(curTime-lastTime):0
// 100个时间差来采样得出帧率
if(sampling.length<100){
sampling.push(fps)
}else{
lastSample = Math.floor(sampling.reduce((t,c)=>t+c, 0)/sampling.length)+'fps'
sampling = []
}
let str =
`${new Date()}----
平均采样帧率:${lastSample}
`
$('#text').text(str)
window.requestAnimationFrame(step);
lastTime = curTime
}
第二种 (优化版)
……