Skip to content

Instantly share code, notes, and snippets.

@andershaig
Created November 9, 2011 20:36
Show Gist options
  • Save andershaig/1352920 to your computer and use it in GitHub Desktop.
Save andershaig/1352920 to your computer and use it in GitHub Desktop.
Live Countdown
<script type="text/javascript">
var end = new Date('6 Oct 2011 23:59:59'); // Timer Expiration
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24;
var timer;
function showRemaining() {
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
var now = new Date();
var distance = end - now;
if (distance < 0 ) {
// handle expiry here..
clearInterval( timer ); // stop the timer from continuing ..
return; // break out of the function so that we do not update the counters with negative values..
}
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 );
days = zeroPad(days, 2);
hours = zeroPad(hours, 2);
minutes = zeroPad(minutes, 2);
seconds = zeroPad(seconds, 2);
// Change this selector to use the ID of the element you want the countdown in.
document.getElementById('countdown').innerHTML = '' + days + ':' + hours + ':' + minutes + ':' + seconds;
}
timer = setInterval(showRemaining, 1000);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment