Last active
January 29, 2017 16:16
-
-
Save Lee182/c8c509fc2431c0a0a1d98b266878f491 to your computer and use it in GitHub Desktop.
get frame rate using requestAnimationFrame and promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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