Skip to content

Instantly share code, notes, and snippets.

@Lee182
Last active January 29, 2017 16:16
Show Gist options
  • Save Lee182/c8c509fc2431c0a0a1d98b266878f491 to your computer and use it in GitHub Desktop.
Save Lee182/c8c509fc2431c0a0a1d98b266878f491 to your computer and use it in GitHub Desktop.
get frame rate using requestAnimationFrame and promises
function getFrameRate(ms) {
var i = -1
var run = true
function tick() {
if (run === false) {return}
i++
window.requestAnimationFrame(tick)
}
return new Promise(function(resolve){
tick()
setTimeout(function(){
run = false
resolve( (i/ms) * 1000 )
}, ms)
})
}
// usage
getFrameRate(1000).then(function(res){
console.log(res)
console.log(res === 60)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment