Created
May 26, 2014 16:25
-
-
Save Lif3line/bb6aea91627943e1b858 to your computer and use it in GitHub Desktop.
A custom timer that can be paused and restarted
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
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