Skip to content

Instantly share code, notes, and snippets.

@ethyde
Created September 30, 2014 12:00
Show Gist options
  • Save ethyde/b2ee70c0a265bd831e64 to your computer and use it in GitHub Desktop.
Save ethyde/b2ee70c0a265bd831e64 to your computer and use it in GitHub Desktop.
Simple Javascript count down timer.
<div id="countdown"></div>
<div id="newcountdown"></div>
CountDownTimer('05/19/2014 10:1 AM', 'countdown');
CountDownTimer('06/20/2014 10:1 AM', 'newcountdown');
function CountDownTimer(dt, id)
{
var end = new Date(dt);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'EXPIRED!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById(id).innerHTML = days + 'days ';
document.getElementById(id).innerHTML += hours + 'hrs ';
document.getElementById(id).innerHTML += minutes + 'mins ';
document.getElementById(id).innerHTML += seconds + 'secs';
}
timer = setInterval(showRemaining, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment