Skip to content

Instantly share code, notes, and snippets.

@codeeshop-oc
Last active March 9, 2022 09:29
Show Gist options
  • Save codeeshop-oc/1d115513ec0fdb5c4068af79da6b93be to your computer and use it in GitHub Desktop.
Save codeeshop-oc/1d115513ec0fdb5c4068af79da6b93be to your computer and use it in GitHub Desktop.
Custom Animate Frame with time delay and function
window.animateFrame = function(delay = 0, func = () => {}) {
let start = Date.now(), // starting time
myReq;
if(typeof func != 'function') {
throw new Error('Not a function')
}
function loop() {
// check if timer is expired
if (Date.now() - start < delay) {
console.log('animating')
myReq = requestAnimationFrame(loop);
} else {
cancelAnimationFrame(myReq)
func()
}
}
loop(); // fire the initial loop
}
// 1 second animate is 1000
animateFrame(1000)
// 1 second with End timer function
animateFrame(1000, () => console.log('End timer function'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment