Last active
November 20, 2019 22:23
-
-
Save julienetie/4e61e9164e4fc035b3cf to your computer and use it in GitHub Desktop.
Delay a callback without setTimeout. Make sure to polyfill RAF.
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
| const delay = (callback, duration) => { | |
| let startTime = 0; | |
| let terminate = false; | |
| const raf = requestAnimationFrame; | |
| const loop = (timestamp) => { | |
| startTime = startTime || timestamp; | |
| if (timestamp > startTime + duration && !terminate && callback) | |
| return callback(),terminate = true; | |
| raf(loop); | |
| } | |
| raf(loop); | |
| } | |
| /* | |
| delay(function () { | |
| alert('Hello World'); | |
| }, 3000); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment