Last active
March 9, 2022 09:29
-
-
Save codeeshop-oc/1d115513ec0fdb5c4068af79da6b93be to your computer and use it in GitHub Desktop.
Custom Animate Frame with time delay and function
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
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