I am currently running a Christmas countdown on my website, but I'm struggling to make it stop once the exact date is reached.
function countdown(){
var now = new Date();
var eventDate = new Date(2016, 11, 25);
var currentTime = now.getTime();
var eventTime = eventDate.getTime();
// remaining time in miliseconds
var remTime = eventTime - currentTime;
// converting into seconds, minutes, hours, days
var s = Math.floor(remTime / 1000);
var m = Math.floor(s / 60);
var h = Math.floor(m / 60);
var d = Math.floor(h / 24);
// finding exact hours, minutes and seconds
h %= 24;
m %= 60;
s %= 60;
d = (d < 10) ? "0" + d : d;
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
document.getElementById("days").innerHTML = d;
document.getElementById("hours").innerHTML = h;
document.getElementById("minutes").innerHTML = m;
document.getElementById("seconds").innerHTML = s;
setInterval(countdown, 1000);
}
countdown();
body {
background: #1f262e;
}
.countdownContainer{
position: absolute; top: 50%; left: 50%;
transform : translateX(-50%) translateY(-50%);
text-align: center;
color: #eff0f2;
padding: 10px;
}
.info {
font-size: 80px;
}
#days, #hours, #minutes, #seconds {
background: #0F1A20;
box-shadow: 0 0 5px 3px #1f262e;
font-size: 120px;
padding: 20px;
}
.title {
font-size: 20px;
}
<table class="countdownContainer" cellspacing="10">
<tr class="title">
<td style="padding-bottom: 20px">DAYS</td>
<td style="padding-bottom: 20px">HOURS</td>
<td style="padding-bottom: 20px">MINUTES</td>
<td style="padding-bottom: 20px">SECONDS</td>
</tr>
<tr class="info" >
<td id="days" border-spacing="10px"></td>
<td id="hours"></td>
<td id="minutes"></td>
<td id="seconds"></td>
</tr>
</table>
I have searched for solutions online, and many suggest using clearInterval
, but I am unsure how to implement it in this context.
Thank you