Skip to content

Instantly share code, notes, and snippets.

@Lif3line
Created May 26, 2014 16:25
Show Gist options
  • Save Lif3line/bb6aea91627943e1b858 to your computer and use it in GitHub Desktop.
Save Lif3line/bb6aea91627943e1b858 to your computer and use it in GitHub Desktop.
A custom timer that can be paused and restarted
function timer(callback, timeOut) {
var store, timeStarted; // Use store for holding data to allow clearTimeout
var remaining = timeOut;
this.start = function() {
timeStarted = new Date();
store = setTimeout(callback, remaining);
}
this.pause = function() {
clearTimeout(store);
remaining -= new Date() - timeStarted;
}
this.updateTimeOut = function(newTimeOut){
clearTimeout(store);
store = setTimeout(callback, newTimeOut);
}
this.start()
}
timer = new timer(function() {
/* Do Something */
}, timeOut);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment