JavaScript - Digital Stop Timer

In this tutorial we will create a Digital Stop Timer using JavaScript. This code will display a digital timer when the user click the start button. The code use onclick() to call a method that will start the timer using setTimeout(). This is a free program, you can modify it and use it as your own. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, 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 - Digital Stop Timer</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <center><button class="btn btn-success" onclick="startTimer(this)">START</button></center>
  18.                         <br />
  19.                         <center><button class="btn btn-danger" onclick="stopTimer()">STOP</button></center>
  20.                         <br />
  21.                         <center><button class="btn btn-warning" onclick="resetTimer()">RESET</button></center>
  22.                 </div>
  23.                 <div class="col-md-8">
  24.                         <center><h1 id="timer" style="font-size:72px; background-color:#000; color:green; border:10px SOLID #ccc;">00:00:00</h1></center>
  25.                 </div>
  26.         </div>
  27.        
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>

Creating the Script

This code contains the script of the application. This code will start the timer when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var display = document.getElementById('timer');
  2. var secs = 0;
  3. var mins = 0;
  4. var hrs  = 0;
  5. var h = "";
  6. var m = "";
  7. var s = "";
  8. var timer;
  9.  
  10. function startTimer(btn){
  11.         btn.setAttribute('disabled', 'disabled');
  12.         durationTime();
  13.        
  14. }
  15.  
  16.  
  17. function stopTimer(){
  18.         document.getElementsByClassName('btn-success')[0].removeAttribute('disabled');
  19.         clearTimeout(timer);
  20. }
  21.  
  22. function resetTimer(){
  23.         document.getElementsByClassName('btn-success')[0].removeAttribute('disabled');
  24.         clearTimeout(timer);
  25.         display.innerHTML = "00:00:00";
  26.         secs = 0;
  27.         mins = 0;
  28.         hrs = 0;
  29.         h = "";
  30.         m = "";
  31.         s = "";
  32. }
  33.  
  34. function countTimer(){
  35.         secs++;
  36.        
  37.         if(secs >= 60){
  38.                 secs = 0;
  39.                 mins++;
  40.                 if(mins >= 60){
  41.                         mins = 0;
  42.                         hrs++;
  43.                 }
  44.         }
  45.        
  46.         h = hrs ? hrs > 9 ? hrs : "0" + hrs : "00";
  47.         m = mins ? mins > 9 ? mins : "0" + mins : "00";
  48.         s = secs > 9 ? secs : "0" + secs;
  49.        
  50.         display.innerHTML = h+":"+m+":"+s;
  51.        
  52.         durationTime();
  53. }
  54.  
  55. function durationTime(){
  56.         if(hrs != 99){
  57.                 timer = setTimeout(countTimer, 100);
  58.         }
  59.        
  60. }
There you have it we successfully created a Digital Stop 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