How to Create Digital Stop Timer in JavaScript

How to Create Digital Stop Timer in JavaScript

Introduction

In this tutorial we will create a How to Create Digital Stop Timer in JavaScript. This tutorial purpose is to teach you on how create a digital stop timer This will cover all the important functionality that allow you add a stop timer to your page content. 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 to add digital stop timer feature. I will give my best to provide you the easiest way of creating this program Digital Stop 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 Stop Timer and the Buttons. 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. </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 Digital Stop Timer</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-2"></div>
  17. <div class="col-md-8">
  18. <center><h1 id="timer" style="font-size:72px; background-color:#000; color:green; border:10px SOLID blue;">00:00:00</h1></center>
  19. <center><button class="btn btn-success" onclick="startTimer(this)">START</button> <button class="btn btn-danger" onclick="stopTimer()">STOP</button> <button class="btn btn-warning" onclick="resetTimer()">RESET</button></center>
  20. </div>
  21. </div>
  22.  
  23. <script src="script.js"></script>
  24. </body>
  25. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will display the stop timer and will start the timer when click the start button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let display = document.getElementById('timer');
  2. let secs = 0;
  3. let mins = 0;
  4. let hrs = 0;
  5. let h = "";
  6. let m = "";
  7. let s = "";
  8. let 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. }

In the code above above we just set several variables that will hold the time elements such as hour, minuet, etc. We created several methods namely stopTimer(), resetTimer(), countTimer(), durationTime(). The durationTime() will handle the duration of time on how long the timer will last. The countTimer() is the one the handle the running timer of timer when triggered. And then stopTimer() will immediately stop the countdown when a certain event is trigger. Lastly the resetTimer() will reset the current timer to its original state.

Output:

The How to Create Digital Stop Timer 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 Digital Stop Timer 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