JavaScript - Simple Countdown Timer

In this tutorial we will create a Simple Countdown Timer using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Countdown Timer</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <form class="form-inline">
  17.                         <label>Set Datetime</label>
  18.                         <input type="date" id="date"/>
  19.                         <input type="time" id="time"/>
  20.                         <button type="button" class="btn btn-primary" onclick="setCount()">Start Countdown</button>
  21.                 </form>
  22.                 <center><h2 id="display">00:00:00</h2></center>
  23.         </div>
  24. </body>
  25. <script src="js/script.js"></script>
  26. </html>

Creating the Script

This code contains the script of the application. This code will generate a countdown timer base on the input value.To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function setCount(){
  2.         var date = document.getElementById('date');
  3.         var time = document.getElementById('time');
  4.        
  5.         if(date.value == "" || time.value == ""){
  6.                 alert("Please complete the required field!");
  7.         }else{
  8.                 var countDate = new Date(date.value+" "+time.value).getTime();
  9.        
  10.                 var countDay = new Date(date.value+" "+time.value).getDate();
  11.        
  12.                 var x = setInterval(function(){
  13.                         var nowTime = new Date().getTime();
  14.                         var nowDate = new Date().getDate();
  15.                         var distance = countDate - nowTime;
  16.                         var dayDis = countDay - nowDate;
  17.                        
  18.                         var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  19.                         var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  20.                         var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  21.                        
  22.                         document.getElementById("display").innerHTML = dayDis + "d " + hours + "h "
  23.                         + minutes + "m " + seconds + "s ";
  24.                        
  25.                        
  26.                         if (distance < 0) {
  27.                                 clearInterval(x);
  28.                                 document.getElementById("display").innerHTML = "Time Expired";
  29.                         }
  30.                        
  31.                 }, 1000);
  32.         }
  33. }
There you have it we successfully created a Simple Countdown Timer using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment