diff --git a/index.html b/index.html index 43832bc..b9676c0 100644 --- a/index.html +++ b/index.html @@ -13,29 +13,33 @@ // Set the date we're counting down to var countDownDate = new Date("Dec 31, 2022 11:59:59").getTime(); -// Update the count down every 1 second -var x = setInterval(function() { +function updateCountdown() { + // Get today's date and time + var now = new Date().getTime(); + + // Find the distance between now and the count down date + var distance = countDownDate - now; - // Get today's date and time - var now = new Date().getTime(); + // Time calculations for days, hours, minutes and seconds + 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)); + var seconds = Math.floor((distance % (1000 * 60)) / 1000); - // Find the distance between now and the count down date - var distance = countDownDate - now; + // Display the result in the element with id="demo" + document.getElementById("demo").innerHTML = days + " days, " + hours + " hours " + + minutes + " minutes, and " + seconds + " seconds."; - // Time calculations for days, hours, minutes and seconds - 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)); - var seconds = Math.floor((distance % (1000 * 60)) / 1000); + // If the count down is finished, write some text + if (distance < 0) { + clearInterval(x); + document.getElementById("demo").innerHTML = "EXPIRED"; + } +} - // Display the result in the element with id="demo" - document.getElementById("demo").innerHTML = days + " days, " + hours + " hours " - + minutes + " minutes, and " + seconds + " seconds."; +// Update the count down initially (avoids waiting a single second before first update) +updateCountdown() - // If the count down is finished, write some text - if (distance < 0) { - clearInterval(x); - document.getElementById("demo").innerHTML = "EXPIRED"; - } -}, 1000); +// Update the count down every 1 second +var x = setInterval(updateCountdown, 1000);