How to Create a Countdown Timer App in JavaScript

How to Create a Countdown Timer App in JavaScript

Introduction

In this tutorial we will create a How to Create a Countdown Timer App in JavaScript. This tutorial purpose is to teach you how to create a countdown timer. This will cover all the important functionality that allow you to start a timer. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want add a countdown timer. I will give my best to provide you the easiest way of creating this program Countdown Timer. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html form inputs and the Timer. To create this simply copy and write it into your text editor, then save it 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. </a>            </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Create a Countdown Timer App</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <center><h1 id="result"></h1></center>
  18.                 </div>
  19.                 <div class="col-md-4">
  20.                         <div class="form-group">
  21.                                 <label>Enter a seconds</label>
  22.                                 <input type="number" id="count" class="form-control"/> 
  23.                         </div>
  24.                         <center><button class="btn btn-primary" onclick="startTimer(this);">Start Timer</button></center>
  25.                 </div>
  26.         </div>
  27. </body>
  28. <script src="script.js"></script>
  29. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will start the timer when you click the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function startTimer(btn){
  2.         var count=document.getElementById("count").value;
  3.        
  4.         if(count==0){
  5.                 alert("Please enter a valid number!");
  6.         }else{
  7.                 document.getElementById("count").value=""
  8.                 btn.setAttribute('disabled', 'disabled');
  9.                 var counter = setInterval(function(){
  10.                
  11.                         count = count - 1;
  12.                         if (count == -1) {
  13.                                 clearInterval(counter);
  14.                                 btn.removeAttribute("disabled");
  15.                                 return;
  16.                         }
  17.                
  18.                         var seconds = count % 60;
  19.                         var minutes = Math.floor(count / 60);
  20.                         var hours = Math.floor(minutes / 60);
  21.                         minutes %= 60;
  22.                         hours %= 60;
  23.                
  24.                         document.getElementById("result").innerHTML = "<span class='text-primary'>"+hours+"</span>" + "H " + "<span class='text-primary'>"+minutes+"</span>" + "min  " + "<span class='text-primary'>"+seconds+"</span>" + "sec";
  25.  
  26.                 }, 1000);
  27.         }
  28. }

In the code above we only created one method that will start the countdown timer called startTimer(). Inside this method we just set multiple conditional statement to handle the runtime of the code structure. To start a timer setInterval() function that will continue to run until it reach the set condition. We create a function that will handle the timer when it reaches to zero by clearing the interval of the timer. We use a variable that set the value of each time component that we will be using for the timer.

Output:


The How to Create a Countdown Timer App in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Countdown Timer App in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment