Skip to content

Instantly share code, notes, and snippets.

@pkqk
Created June 18, 2014 15:48
Show Gist options
  • Save pkqk/16ea2436101f7b3f9530 to your computer and use it in GitHub Desktop.
Save pkqk/16ea2436101f7b3f9530 to your computer and use it in GitHub Desktop.
function timeout(callback, wait) {
var cancel = window.setTimeout(callback, wait);
return function() {
window.clearTimeout(cancel);
}
}
cancel_the_timeout = timeout(function() { alert("hi") }, 3600);
cancel_the_timeout();
@nickludlam
Copy link

Yeah that's a lovely pattern. I ended up slightly augmenting it to allow inspection of state from outside, as well as cancelling the timer.

return {
  running: function() { return timerID },
  cancel: function() { window.clearTimeout(timerID); timerID = null; }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment