Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Created June 16, 2017 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/8f815591372342b2d47b4dfacc974732 to your computer and use it in GitHub Desktop.
Save bradoyler/8f815591372342b2d47b4dfacc974732 to your computer and use it in GitHub Desktop.
retry a function with a given timeout, using requestAnimationFrame
function retryRAF (fn, delay) {
var start = Date.now();
function loop () {
if (Date.now() - start < delay) {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(loop);
fn();
}
}
}
loop();
}
/* usage
function log() {
console.log('test', new Date());
}
retryRAF(log, 1000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment