JavaScript - Simple Time Counter

In this tutorial we will create a Simple Time Counter using JavaScript. This code will start a timer that have been set when the user click the start timer button. The code use onclick() function to launch a specific function that will start a timer by calling the setInterval() a built-in javascipt function with a duration of 1000 is equivalent to 1 seconds. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

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. </a>            </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Time Counter</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-group">
  18.                                 <label>Enter a number</label>
  19.                                 <input type="number" id="count" class="form-control"/> 
  20.                         </div>
  21.                         <center><button class="btn btn-primary" onclick="startTimer(this);">Start Timer</button></center>
  22.                 </div>
  23.                 <div class="col-md-8">
  24.                         <center><h3 id="result"></h3></center>
  25.                 </div>
  26.         </div>
  27. </body>
  28. <script src="js/script.js"></script>
  29. </html>

Creating the Script

This code contains the script of the application. This code will start a timer when the button is clicked. 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 directory
  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>" + "hours " + "<span class='text-primary'>"+minutes+"</span>" + "minutes and " + "<span class='text-primary'>"+seconds+"</span>" + "seconds";
  25.  
  26.                 }, 1000);
  27.         }
  28. }
There you have it we successfully created a Simple Time Counter 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