Count down timer - Javascript
HTML
<div class="counter_outer">
<ul>
<li>
<ul>
<li><span class="daysNumber">00</span></li>
<li class="daysText">Days</li>
</ul>
</li>
<li>
<ul>
<li><span class="hoursNumber">00</span></li>
<li class="hoursText">Hours</li>
</ul>
</li>
<li>
<ul>
<li><span class="minutesNumber">00</span></li>
<li class="minutesText">Minutes</li>
</ul>
</li>
</ul>
<div class="singleLine"></div>
</div>
Javascript
function countdownTimeStart() {
var countDownDate = new Date("Sep 14, 2021 23:59:59").getTime();
// Update the count down every 1 second
var x = setInterval(function () {
var now = new Date().getTime();
var distance = countDownDate - now;
seconds = distance / 1000;
var d = Math.floor(seconds / (3600 * 24));
var h = Math.floor(seconds % (3600 * 24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
d = d > 0 ? d : "00";
h = h > 0 ? h : "00";
m = m > 0 ? m : "00";
s = s > 0 ? s : "00";
var dDisplay = d == 1 ? " day" : " days"
var hDisplay = h == 1 ? " hour" : " hours"
var mDisplay = m == 1 ? " minute" : " minutes"
var sDisplay = s == 1 ? " second" : " seconds"
document.querySelector(".counter_outer .daysNumber").innerText = String(d).padStart(2, "0");
document.querySelector(".counter_outer .hoursNumber").innerText = String(h).padStart(2, "0");
document.querySelector(".counter_outer .minutesNumber").innerText = String(m).padStart(2, "0");
document.querySelector(".counter_outer .daysText").innerText = dDisplay;
document.querySelector(".counter_outer .hoursText").innerText = hDisplay;
document.querySelector(".counter_outer .minutesText").innerText = mDisplay;
if (distance < 0) {
clearInterval(x);
}
}, 1000);
}
Comments
Post a Comment