Created
November 9, 2011 21:06
-
-
Save andershaig/1353027 to your computer and use it in GitHub Desktop.
Live Countdown from Countdown Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
var days_left = {{ time_clock.days }}; | |
var hours_left = {{ time_clock.hours }}; | |
var minutes_left = {{ time_clock.minutes }}; | |
var _now = new Date(); | |
var end = new Date(_now.getTime() + (minutes_left * 60 * 1000) ); | |
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