Creating a Stopwatch Web Application using JavaScript Tutorial

In this tutorial, you can learn to create a simple Stopwatch web application using JavaScript. This tutorial aims to provide students and beginners with a reference for learning to create a Stopwatch. Here, I will be providing a simple web page script that demonstrates the creation of a simple Stopwatch Web Application.

What is Stopwatch Web App

It is a simple web application that simulates the Stopwatch functionalities. Stopwatch web application allows the user to start, pause/stop, and reset time measurement between the time activation (0) and deactivation (n). Stopwatches are often used for a game with a time races scenario.

How to Create a Stopwatch Web Application?

The Stopwatch web application can be easily achieved using JavaScript's built methods and APIs. To create stopwatch functionalities, we can use the setInterval function/method, date, and some of the formulas to calculate the difference between the start time and end time. Also, we will need to manipulate the stopwatch container element text to update the current measurement of the time every millisecond/second. Check out the web page scripts that I provided below to understand more about how to create a simple stopwatch web application.

Sample Web Page Scripts

The scripts below result in a simple web application that contains a stopwatch time measurement container, start button, pause button, and reset button elements. The application allows the user to start the stopwatch from zero and pause/continue/reset the time measurement.

Page Interface

Here is the HTML file script known as index.html. It contains the HTML codes for the page layout and the stopwatch containers and button elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>JS - Stopwatch</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11. <link rel="stylesheet" href="style.css">
  12. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  13. </head>
  14. <div class="content-md-lg py-3">
  15. <div class="page-title">Creating a Stopwatch using JavaScript</div>
  16. <hr style="margin:auto; width:25px" class="border-light opacity-100">
  17. <div class="container-lg">
  18. <div class="row py-3">
  19. <!-- Stopwatch Wrapper -->
  20. <div id="stopwatch-wrapper" class="col-lg-5 col-md-5 col-sm-10 col-12 mx-auto">
  21. <div class="card bg-dark rounded-0 border-dark-subtle">
  22. <div class="card-body rounded-0">
  23. <div class="container-fluid">
  24. <div id="timer-wrapper">
  25. <div class="timer">00:00:00</div>
  26. </div>
  27. <div id="timer-buttons">
  28. <button type="button" id="startBTN" class="timer-btn btn btn-dark border border-dark-subtle rounded-pill text-info"><span class="material-symbols-outlined">play_arrow</span> Start</button>
  29. <button type="button" id="pauseBTN" class="timer-btn btn btn-dark border border-dark-subtle rounded-pill text-info"><span class="material-symbols-outlined">pause</span> Pause</button>
  30. <button type="button" id="resetBTN" class="timer-btn btn btn-dark border border-dark-subtle rounded-pill text-info"><span class="material-symbols-outlined">restart_alt</span> Reset</button>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. <!-- Stopwatch Wrapper -->
  37. </div>
  38. </div>
  39. </div>
  40. <script src="script.js"></script>
  41. </body>
  42. </html>

Stylesheet

Next, here is the custom stylesheet or the CSS file script known as style.css. It contains the custom codes for some of the page elements.

  1. @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Space+Mono&display=swap" rel="stylesheet');
  2. :root{
  3. --space-mono-font: 'Space Mono', monospace;
  4. --border-dark-subtle: #373838;
  5. }
  6. *{
  7. box-sizing: border-box;
  8. }
  9. body *{
  10. font-family: var(--space-mono-font);
  11. }
  12. /**
  13. Page Design
  14. */
  15. body,
  16. html{
  17. height: 100%;
  18. width: 100%;
  19. margin: 0;
  20. padding: 0;
  21. }
  22. body{
  23. background-color: #282A3A;
  24. }
  25. .page-title{
  26. font-size: 2.5rem;
  27. font-weight: 500;
  28. color: #fff;
  29. letter-spacing: 3px;
  30. font-family: var(--secular-font);
  31. text-align: center;
  32. text-shadow: 0px 0px 3px #2020208c;
  33. }
  34. .border-dark-subtle{
  35. border-color: var(--border-dark-subtle) !important;
  36. }
  37.  
  38.  
  39. /* Timer */
  40. #timer-wrapper{
  41. width: 300px;
  42. height: 300px;
  43. margin: auto;
  44. border: 1px solid #2a2a2a;
  45. box-shadow: 0px 0px 10px #a0e1ffa1;
  46. padding: 0.5em;
  47. border-radius: 50% 50%;
  48. display: flex;
  49. align-items: center;
  50. justify-content: center;
  51. }
  52.  
  53. #timer-wrapper .timer{
  54. color: #fff;
  55. font-weight: 600;
  56. letter-spacing: 3px;
  57. font-size: 2.5rem;
  58. }
  59. /* timer-wrapper buttons */
  60. #timer-buttons {
  61. width: fit-content;
  62. margin: 1em auto;
  63. display: flex;
  64. }
  65.  
  66. #timer-buttons .timer-btn{
  67. display: none;
  68. align-items: center;
  69. margin: 0 .5em;
  70. justify-content: center;
  71. }

Main Script

Lastly, here is the Javascript file script that is known as script.js. It contains the JS codes that make the stopwatch and its buttons functional.

  1. // Buttons Selector
  2. const startBTN = document.getElementById('startBTN')
  3. const pauseBTN = document.getElementById('pauseBTN')
  4. const resetBTN = document.getElementById('resetBTN')
  5.  
  6. const TimerField = document.querySelector('#timer-wrapper .timer')
  7.  
  8. var timerStart = 0,
  9. now;
  10. window.addEventListener('load', ()=>{
  11. startBTN.style.display = `flex`
  12. })
  13.  
  14. class Timer{
  15. elapsedTime;
  16. currentTime;
  17. started;
  18. timerInterval;
  19. constructor(){
  20. this.elapsedTime = 0
  21. }
  22. timerIntervalFunc=()=>{
  23. console.log(this.currentTime)
  24. this.currentTime = Date.now() - this.started;
  25. }
  26. timerStart (){
  27. this.started = Date.now() - this.elapsedTime ;
  28. startBTN.innerHTML = `<span class="material-symbols-outlined">resume</span> Continue`
  29. startBTN.style.display = `none`
  30. pauseBTN.style.display = `flex`
  31. resetBTN.style.display = `flex`
  32. this.timerInterval = setInterval(()=>{
  33. this.currentTime = Date.now() - this.started;
  34. this.displayTimer()
  35. }, 0)
  36. }
  37.  
  38. displayTimer(){
  39. var timerData = this.currentTime
  40. this.elapsedTime = timerData
  41. var difMin = timerData / (1000 * 60);
  42. var min = Math.floor(difMin)
  43. var difsec = (difMin - min) * 60;
  44. var sec = Math.floor(difsec)
  45. var difms = 100 * (difsec - sec);
  46. var ms = Math.floor(difms)
  47. min = String(min).padStart(2, 0)
  48. sec = String(sec).padStart(2, 0)
  49. ms = String(ms).padStart(2, 0)
  50.  
  51. TimerField.innerText = `${min}:${sec}:${ms}`
  52. }
  53. pauseTimer(){
  54. clearInterval(this.timerInterval)
  55. startBTN.style.display = `flex`
  56. pauseBTN.style.display = `none`
  57. }
  58. resetTimer(){
  59. clearInterval(this.timerInterval)
  60. startBTN.innerHTML = `<span class="material-symbols-outlined">play_arrow</span> Start`
  61. startBTN.style.display = `flex`
  62. pauseBTN.style.display = `none`
  63. resetBTN.style.display = `none`
  64. this.elapsedTime = 0
  65. TimerField.innerText = `00:00:00`
  66. }
  67. }
  68. let TimerObj = new Timer();
  69. window.addEventListener('load', ()=>{
  70. startBTN.style.display = `flex`;
  71. })
  72. startBTN.addEventListener('click', ()=>{
  73. TimerObj.timerStart()
  74. })
  75. pauseBTN.addEventListener('click', ()=>{
  76. TimerObj.pauseTimer()
  77. })
  78. resetBTN.addEventListener('click', ()=>{
  79. TimerObj.resetTimer()
  80. })

Snapshots

Here are the snapshots or images of the overall result of the given stopwatch web app scripts above.

Not Started

Stopwatch Web Application using JavaScript

Started

Stopwatch Web Application using JavaScript

DEMO VIDEO

There you go! I have also provided the complete source code zip file of the web page scripts that I created on this site and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you wanted.

That's it! I hope that this Creating a Stopwatch using JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment