Created
October 12, 2018 19:57
-
-
Save SMUsamaShah/2847efebe6b1fd3911734ac718ff9c5c to your computer and use it in GitHub Desktop.
self adjusting setTimeout
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
setTimeout.dt = 0; | |
function selfAdjustingTimeout(callback, timeout, args) { | |
setTimeout.et = (new Date).getTime() + timeout; | |
let cb = () => { | |
setTimeout.dt = (new Date).getTime() - setTimeout.et; | |
callback(); | |
}; | |
setTimeout(cb, timeout - setTimeout.dt, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment