Vanilla JavaScript countdown clock

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Nice tutorial.
I did almost same thing a while back. Mine counts down from 24 hours. When it reaches 0, it resets back to 24 hours.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

A time ago, we made this cool year loading bar, and this made me think a countdown timer would also be cool.
Since I'm getting married next year, let's use that as an example.
We will be creating an end date, and every second we will check how long away this is.
What you'll learn in this article:
The end result will look like this:
Let's start by defining our HTML structure.
<div>
<h1>The big day</h1>
<p>Nicole & Chris wedding</p>
<p id="done"></p>
<ul>
<li><span id="days">0</span> Days</li>
<li><span id="hours">0</span> Hours</li>
<li><span id="minutes">0</span> Minutes</li>
<li><span id="seconds">0</span> Seconds</li>
</ul>
</div>
We will have a title and intro paragraph, as well as a empty done div.
This done div will be used if the timer expired.
Then we have a list with days, hours, minutes, and seconds. Default on 0 in case the date expired.
Now ofcourse we want this too look a bit nicer.
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
font-family: Roboto, "Helvetica Neue", Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
background: #8fbfe0;
}
h1 {
font-size: 3rem;
margin-top: 0;
}
ul {
display: flex;
list-style: none;
margin: 0;
padding: 0;
margin-top: 2rem;
}
ul li {
background: #7c77b9;
border-radius: 10px;
padding: 1rem;
display: flex;
flex-direction: column;
margin: 0 0.5rem;
color: #8fbfe0;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5);
}
ul li span {
font-size: 2rem;
}
What you'll see is that we remove the main styling from the ul to use Flexbox to center it, and space the elements.
Then we add a box-shadow and some colors to make it pop more.
Now the JavaScript part.
Let's first define our end date:
const end = new Date("May 03, 2021 00:00:00").getTime();
We define the date as a Date object and use the getTime function to get the milliseconds. We do this because it's easier to count with.
Now let's get all our output span elements.
const dayEl = document.getElementById('days');
const hoursEl = document.getElementById('hours');
const minutesEl = document.getElementById('minutes');
const secondsEl = document.getElementById('seconds');
The last set of variables we can define to make it easier for ourselves are the time fragment pieces.
const seconds = 1000;
const minutes = seconds * 60;
const hours = minutes * 60;
const days = hours * 24;
What this means:
Now we will need to create a setInterval function to run every second.
const x = setInterval(function () {
// Code here
}, seconds);
We attach it to a const to be able to unset it if it's no longer needed.
Now, let's get the current timestamp and a difference between our end date and now.
let now = new Date().getTime();
const difference = end - now;
The difference will now have the difference in milliseconds between now and our set date.
Let's first check if it's not already expired.
if (difference < 0) {
clearInterval(x);
document.getElementById("done").innerHTML = "We're married! π";
return;
}
Here, we check if the difference is smaller then 0, then we clear our interval so it won't run again.
We also use a return to stop the rest of the function from running.
Now all that's left is to show the correct numbers for each element.
dayEl.innerText = Math.floor(difference / days);
hoursEl.innerText = Math.floor( (difference % days) / hours );
minutesEl.innerText = Math.floor( (difference % hours) / minutes );
secondsEl.innerText = Math.floor( (difference % minutes) / seconds );
As mentioned in our example we return a floored value of the difference converted to each respectable element.
Note the use of the
%is to get a remained back.
For example, let's say our difference in milliseconds is 15091810828.
This might be a bit much. I'd advise you to even write down one number and calculate it on paper to get a good feel for what's happening.
There we go, we now have a countdown timer in JavaScript, let me know what you create with this cool piece of code.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter