Skip to content

Instantly share code, notes, and snippets.

@collinprice
Created April 17, 2015 18:32
Show Gist options
  • Save collinprice/06d2dc34ff99e98a1f7f to your computer and use it in GitHub Desktop.
Save collinprice/06d2dc34ff99e98a1f7f to your computer and use it in GitHub Desktop.
Simple timer with callback.
function Timer(timerLength, countdownCallback) {
var intervalId = null,
currentInterval;
this.start = function() {
if (timerLength <= 0) return false;
currentInterval = timerLength;
intervalId = setInterval(function() {
currentInterval--;
countdownCallback(currentInterval);
if (currentInterval <= 0) {
clearInterval(intervalId);
}
}, 1000);
};
this.stop = function() {
clearInterval(intervalId);
};
this.restart = function() {
clearInterval(intervalId);
this.start();
};
}
module.exports = Timer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment