Skip to content

Instantly share code, notes, and snippets.

@edfuh
Created March 9, 2013 23:01
Show Gist options
  • Save edfuh/5126168 to your computer and use it in GitHub Desktop.
Save edfuh/5126168 to your computer and use it in GitHub Desktop.
simple countdown timer for some stuff
function Countdown(seconds, update, done) {
this.length = seconds;
this.update = update;
this.done = done;
}
Countdown.prototype.start = function () {
var self = this;
self.update(self.length);
this.timer = setInterval(function () {
if (--self.length < 1) {
clearInterval(self.timer);
self.update(self.length);
self.done();
} else {
self.update(self.length);
}
}, 1e3);
}
Countdown.toClock = function (time) {
return ('0' + Math.floor(time / 60)).slice(-2) + ':' + ('0' + time % 60).slice(-2)
}
document.getElementsByTagName('button')[0].onclick = function () {
(new Countdown(64, function(time) {
timer.innerHTML = Countdown.toClock(time);
}, function() {
timer.innerHTML += 'go';
})).start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment