Skip to content

Instantly share code, notes, and snippets.

@thebrecht
Created June 8, 2012 09:13
Show Gist options
  • Save thebrecht/2894648 to your computer and use it in GitHub Desktop.
Save thebrecht/2894648 to your computer and use it in GitHub Desktop.
Countdown Timer
function CountdownTimer(from, end) {
var ct = this;
var ONE_HOUR = 1000 * 60 * 60;
var ONE_MIN = 1000 * 60;
var ONE_SEC = 1000;
var startFrom = new Date(from);
var endAt = new Date(end);
var diff = endAt - startFrom;
var parseTime = function () {
ct.hour = Math.floor(diff / ONE_HOUR);
if (ct.hour > 0) diff = diff - (ct.hour * ONE_HOUR);
ct.min = Math.floor(diff / ONE_MIN);
if (ct.min > 0) diff = diff - (ct.min * ONE_MIN);
ct.sec = Math.floor(diff / ONE_SEC);
console.log(ct.sec);
startFrom.setSeconds(startFrom.getSeconds() + 1);
diff = endAt - startFrom;
}
this.isTimeup = false;
this.count = function () {
var timer = setInterval(function () {
parseTime();
if (diff <= 0) {
ct.isTimeup = true;
clearInterval(timer);
}
}, 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment