JavaScript - Simple Stopwatch

In this tutorial we will create a Simple Stopwatch 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 Stopwatch</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <center><h1 id="timer" style="font-size:72px;">00:00:00s</h1></center>
  17.                 <br />
  18.                 <div style="text-align:center;">
  19.                         <button class="btn btn-success" onclick="startTimer(this)">START</button>
  20.                         <button class="btn btn-danger" onclick="stopTimer()">STOP</button>
  21.                         <button class="btn btn-warning" onclick="resetTimer()">RESET</button>
  22.                 </div>
  23.         </div>
  24.        
  25. <script src="js/script.js"></script>
  26. </body>
  27. </html>

Creating the Script

This code contains the script of the application. This code will render a virtual stopwatch that can monitor your activity time. 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. 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 countTimer(){
  11.         secs++;
  12.        
  13.         if(secs >= 60){
  14.                 secs = 0;
  15.                 mins++;
  16.                 if(mins >= 60){
  17.                         mins = 0;
  18.                         hrs++;
  19.                 }
  20.         }
  21.        
  22.         h = hrs ? hrs > 9 ? hrs : "0" + hrs : "00";
  23.         m = mins ? mins > 9 ? mins : "0" + mins : "00";
  24.         s = secs > 9 ? secs : "0" + secs;
  25.        
  26.         display.innerHTML = h+":"+m+":"+s+"s";
  27.        
  28.         timerDuration();
  29. }
  30.  
  31. function timerDuration(){
  32.         if(hrs != 99){
  33.                 timer = setTimeout(countTimer, 100);
  34.         }
  35.        
  36. }
  37.  
  38. function startTimer(btn){
  39.         btn.setAttribute('disabled', 'disabled');
  40.         timerDuration();
  41.        
  42. }
  43.  
  44.  
  45. function stopTimer(){
  46.         document.getElementsByClassName('btn-success')[0].removeAttribute('disabled');
  47.         clearTimeout(timer);
  48. }
  49.  
  50. function resetTimer(){
  51.         document.getElementsByClassName('btn-success')[0].removeAttribute('disabled');
  52.         clearTimeout(timer);
  53.         display.innerHTML = "00:00:00s";
  54.         secs = 0;
  55.         mins = 0;
  56.         hrs = 0;
  57.         h = "";
  58.         m = "";
  59.         s = "";
  60. }
There you have it we successfully created a Simple Stopwatch 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