In this article, you will learn how to create a simple countdown timer using HTML CSS, and JavaScript. JavaScript Countdown Timer is a great project for beginners. Timers are used in many places, mainly in different types of e-commerce or business websites. Some products are shown on the eCommerce website through a countdown when they arrive.
This type of Simple Countdown Timer is also used to keep the under-construction website and the website in maintenance mode. It helps to know how long after the service or product they will receive. This maintains quality and user satisfaction on the website.
Many people think of taking the help of such design jQuery or external library. However, in this case, you do not have to take the help of any library.
Simple Javascript Countdown Timer (Live Demo)
I have used HTML to add some basic information. CSS is used to design it and JavaScript is used to make it work. It will basically take the current time from your device using the new data method. Then the countdown will start running by subtracting from your scheduled time.
Below I have given a live demo that will help you to know how this timer works. Here you will find the required source code which you can copy and use in your own work.
See the Pen Javascript Simple Countdown Timer by Raj Template (@RajTemplate) on CodePen.
Simple Countdown Timer using JavaScript
Now learn how to create this JavaScript countdown timer. This requires you to have a basic idea about HTML CSS and JavaScript. No need to worry if you are also a beginner. Because I have given a complete explanation keeping in mind the beginners.
First, you create an HTML and CSS file. Then follow the tutorials below and learn how I made it.
Step 1: Basic design of the project
I have created the basic structure of this project using the following HTML and CSS code. Here we have created an area in which these four boxes will be located.
Step 2: Basic structure of Countdown Timer
Now I have created a small box on the web page. In this type of box, I made four to count the four types of time. At first, I just made a box of days. The box contains a number and contains some text. Box length and height width: 28vmin used. Use align-items: center to keep the information in the middle.
Step 3: Make more boxes
Now in the same way I made the other three boxes. There is no need to use a separate CSS code for this. Because above all we have added CSS code.
Step 4: Make Countdown Timer Responsive with CSS
Now using CSS @media has made it responsive to specific screen sizes. As a result, any device user can easily use the Responsive Countdown Timer. The following codes have helped in the case of laptops.
The code below makes it suitable for tablets.
I have determined how this design can be seen in the case of mobile by the code below.
Step 5: Activate Simple Countdown Timer using JavaScript
Now I have fully implemented this Simple Countdown Timer with the help of JavaScript. Below I have put together the complete JavaScript. If you are a beginner, follow the explanation below.
JavaScript Explanation
Below I have determined the constants of some id functions one by one. Because we can’t use any id function directly in JavaScript.
Now the date has been set for what time we want to run the countdown. Input the time you want to make the timer.
Now I have taken the current time from the device using the new Date method. new Date is a JavaScript method that helps to take the current time from the device.
Now I have saved in remainingTime how long this countdown will last. For this, I have subtracted the current time from the time you set.
Now I have expressed the time of day, hour, minute in milliseconds. Then we store those values in a constant.
Using the code below, I have added one zero to the time of one number, that is, when the time is less than 10, one zero will be added before that number. It will only help to enhance its beauty.
Now I have added a condition using the ‘if’ method. A type of arrow can be found here when your added time is less than the current time. This means that a message will appear when the account arrives at your scheduled time while the account is running. This will basically let you know that your scheduled time has come.
Now I have fully implemented the countdown timer using the following codes. As I said earlier in remainingTime I have stored how long the countdown should run. Now I have divided that time into days, hours, minutes, and seconds.
At first, I published the time at that time. Then I have expressed the time that is more in the form of hours. Even then, what is left is expressed in minutes and the time that is left at the end of all is expressed in the form of seconds.
Now we have arranged to display all this information in the display using textContent. text content will help you see all of this information in specific places on that page.
At the end of it all, now I have been instructed to update this system every 1000 milliseconds using setInterval. Since I have expressed time here in the form of seconds, it is important to update this time every second.
Hopefully from this tutorial, you have learned that a simple countdown timer is similarly created using JavaScript. Earlier I have shown you different types of analog-digital clocks and many more types of JavaScript projects.
Below you will see a download button where you will find the source code needed to create this countdown timer.