Created
April 17, 2015 18:32
-
-
Save collinprice/06d2dc34ff99e98a1f7f to your computer and use it in GitHub Desktop.
Simple timer with callback.
This file contains hidden or 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(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