Difference between revisions of "Widget:Countdown"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
<span><!--{$time}--></span><br /> | <span><!--{$time}--> ET</span><br /> | ||
<span id="countdown"> | <span id="countdown">-1d -1h -1m -1s</span> | ||
<script> | <script> | ||
// | // Modified version of https://www.w3schools.com/howto/howto_js_countdown.asp | ||
var countDownDate = new Date("<!--{$time}--> ET").getTime(); | |||
var countDownDate = new Date("<!--{$time}-->").getTime(); | |||
// Update the count down every 1 second | // Update the count down every 1 second | ||
var x = setInterval(function() { | var x = setInterval(function() { | ||
// Get todays date and time | // Get todays date and time | ||
var now = new Date().getTime(); | var now = new Date().getTime(); | ||
Line 16: | Line 14: | ||
var distance = countDownDate - now; | var distance = countDownDate - now; | ||
// Time calculations for days, hours, minutes | // Time calculations for days, hours, minutes | ||
var days = Math.floor(distance / (1000 * 60 * 60 * 24)); | var days = Math.floor(distance / (1000 * 60 * 60 * 24)); | ||
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | ||
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | ||
// | // Display the result in the element with id="countdown" | ||
if (distance < 0) { | if (days > 0) { | ||
document.getElementById("countdown").innerHTML = "(In about " + days + " days and " + hours + " hours)"; | |||
} else if (days < 0) { | |||
document.getElementById("countdown").innerHTML = "(In about " + hours + " hours and " + minutes + " minutes)"; | |||
} else if (distance < 0) { | |||
clearInterval(x); | clearInterval(x); | ||
document.getElementById("countdown").innerHTML = " | document.getElementById("countdown").innerHTML = "The race has ended."; | ||
} | } | ||
}, 1000); | }, 1000); | ||
</script> | </script> |
Revision as of 03:49, 27 January 2019
ET
-1d -1h -1m -1s
<script> // Modified version of https://www.w3schools.com/howto/howto_js_countdown.asp var countDownDate = new Date(" ET").getTime();
// Update the count down every 1 second var x = setInterval(function() {
// Get todays date and time var now = new Date().getTime();
// Find the distance between now and the count down date var distance = countDownDate - now;
// Time calculations for days, hours, minutes var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
// Display the result in the element with id="countdown" if (days > 0) { document.getElementById("countdown").innerHTML = "(In about " + days + " days and " + hours + " hours)"; } else if (days < 0) { document.getElementById("countdown").innerHTML = "(In about " + hours + " hours and " + minutes + " minutes)"; } else if (distance < 0) { clearInterval(x); document.getElementById("countdown").innerHTML = "The race has ended."; }
}, 1000); </script>