Last active
August 29, 2015 13:57
-
-
Save bocharsky-bw/9863839 to your computer and use it in GitHub Desktop.
Count down timer in seconds
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 BWTimer(_delay, _step) { | |
var self = this; | |
/** | |
* Delay in seconds | |
*/ | |
var delay = _delay; | |
/** | |
* Delay in seconds | |
*/ | |
var step = _step; | |
/** | |
* Timeout in seconds | |
*/ | |
var timeout = _step * 1000; | |
this.setDelay = function(_delay) { | |
delay = _delay; | |
return this; | |
} | |
this.getDelay = function() { | |
return delay; | |
} | |
this.setStep = function(_step) { | |
step = _step; | |
timeout = step * 1000; | |
return this; | |
} | |
this.getStep = function() { | |
return step; | |
} | |
/** | |
* Count down | |
*/ | |
this.countDown = function(stepCallback, endCallback) { | |
if (typeof stepCallback === 'function') { | |
stepCallback(delay); | |
} else { | |
console.log(delay); | |
} | |
setTimeout(function(){ | |
delay -= step; | |
if (delay > 0) { | |
return self.countDown(stepCallback, endCallback); | |
} | |
if (typeof stepCallback === 'function') { | |
stepCallback(delay); | |
} else { | |
console.log(delay); | |
} | |
if (typeof endCallback === 'function') { | |
return endCallback(); | |
} else { | |
alert('Time out!'); | |
} | |
}, timeout); | |
} | |
return this; | |
} | |
var timer = new BWTimer(5, 1); | |
timer.countDown( | |
function(delay){ | |
console.log(delay); | |
}, | |
function(){ | |
alert("It's over!"); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment