Skip to content

Instantly share code, notes, and snippets.

@freaktechnik
Last active July 19, 2016 21:45
Show Gist options
  • Select an option

  • Save freaktechnik/3147bac85a825830d82496788218067c to your computer and use it in GitHub Desktop.

Select an option

Save freaktechnik/3147bac85a825830d82496788218067c to your computer and use it in GitHub Desktop.
Makes sure setTimeout waits long enough.
function ensureSetTimeout(cb, s) {
var callTime = Date.now();
setTimeout(function() {
var cbTime = Date.now();
if(cbTime - s == callTime)
cb();
else
ensureSetTimeout(cb, s - (cbTime - callTime))
}, s);
}
exports.ensureSetTimeout = ensureSetTimeout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment