Skip to content

Commit

Permalink
Move the countdown calculation into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniGrief authored Sep 8, 2022
1 parent 848d80a commit fb01308
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
</script>

0 comments on commit fb01308

Please sign in to comment.