Skip to content

Instantly share code, notes, and snippets.

@bocharsky-bw
Last active August 29, 2015 13:57
Show Gist options
  • Save bocharsky-bw/9863839 to your computer and use it in GitHub Desktop.
Save bocharsky-bw/9863839 to your computer and use it in GitHub Desktop.
Count down timer in seconds
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