Skip to content

Instantly share code, notes, and snippets.

@SMUsamaShah
Created October 12, 2018 19:57
Show Gist options
  • Save SMUsamaShah/2847efebe6b1fd3911734ac718ff9c5c to your computer and use it in GitHub Desktop.
Save SMUsamaShah/2847efebe6b1fd3911734ac718ff9c5c to your computer and use it in GitHub Desktop.
self adjusting setTimeout
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