In this blog, we are going to learn how we can create a simple countdown timer to embed in any website using JavaScript.
First we need to set the date we’re counting down to,
var countDownDate = new Date(“Jan 1, 2050 15:37:25”).getTime();
Next we need to update the count down for every second so we are going to create a function.
var x = setInterval(function() {
Inside the function we are achieving following tasks.
1. Get today’s date and time
var now = new Date().getTime();
Inside the function we are achieving following tasks.
1. Get today’s date and time
var now = new Date().getTime();
2. And then find the distance between now and the count down date
var distance = countDownDate – now;
3. Calculate days, hours, minutes and seconds from now to distant time
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);
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);
4. Now we can show the Output anywhere in the DOM, here we are appending it to an element with id=”demo”
document.getElementById(“demo”).innerHTML = days + “d ” + hours + “h ” + minutes + “m ” + seconds + “s “;
5. Finally if the count down is over, write some text and close the countdown function.
if (distance < 0) {
clearInterval(x);
document.getElementById(“demo”).innerHTML = “EXPIRED”;}
}, 1000);
clearInterval(x);
document.getElementById(“demo”).innerHTML = “EXPIRED”;}
}, 1000);
6. In your html place the following tag where ever you want to show the timer…
<p id=”demo” style=”text-align: center; font-size: 60px; margin-top: 0px;”></p>
Our code looks like this,
<p id=”demo” style=”text-align: center; font-size: 60px; margin-top: 0px;”></p>
<script>
var countDownDate = new Date(“Jan 5, 2050 15:37:25”).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
<script>
var countDownDate = new Date(“Jan 5, 2050 15:37:25”).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate – now;
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);
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById(“demo”).innerHTML = days + “d ” + hours + “h “
+ minutes + “m ” + seconds + “s “;
if (distance < 0) {
clearInterval(x);
document.getElementById(“demo”).innerHTML = “EXPIRED”;
}
}, 1000);
</script>
+ minutes + “m ” + seconds + “s “;
if (distance < 0) {
clearInterval(x);
document.getElementById(“demo”).innerHTML = “EXPIRED”;
}
}, 1000);
</script>
Output:
(Visited 98 times, 1 visits today)