Created
June 16, 2017 16:51
-
-
Save bradoyler/8f815591372342b2d47b4dfacc974732 to your computer and use it in GitHub Desktop.
retry a function with a given timeout, using requestAnimationFrame
This file contains 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 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