Can you create a Stopwatch using JavaScript?

  • DOM Manipulation
  • Event Handiling
  • setInterval() and clearInterval()
  • Basic time ( Hours, Minutes, Seconds ) logic.

Final Output Preview:-

Step 1: HTML Structure for Stopwatch

<div class="stopwatch">
  <div id="hours" class="hours">00</div>
  :
  <div id="minutes" class="minutes">00</div>
  :
  <div id="seconds" class="seconds">00</div>
</div>

<div class="stopwatch-actions">
  <button id="start-stop">Start</button>
  <button id="reset">Reset</button>
</div>

Explanation:

  • We have taken 3 divs HTML Element to display Hours, Minutes, and Seconds
  • Each div has a unique ID so we can update values using JavaScript
  • Two action buttons are used. 1. Start/Stop -> To control (start and stop) the stopwatch and 2. Reset -> To reset timer to 00:00:00

Step 2: CSS (Optional – For Better UI)

.stopwatch {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: #111;
  color: #fff;
  padding: 20px 30px;
  border-radius: 10px;
}

.stopwatch-actions {
  margin-top: 20px;
  text-align: center;
}

button {
  padding: 10px 20px;
  margin: 0 5px;
  cursor: pointer;
}

button.stop {
  background-color: red;
  color: #fff;
}

CSS Explanation:-

  • Flexbox is used to align elements in the center. We use Flexbox in One Dimentional Layout. You can learn more about flexbox here.
  • Background color, padding, and border-radius improve appearance
  • You can skip heavy styling in interviews and focus on logic.

Step 3: JavaScript – Main Logic – To create a Stopwatch

1. Initialize and assign Time ( Hours, Minutes, and Seconds ) Elements

const hoursItem = document.getElementById("hours");
const minutesItem = document.getElementById("minutes");
const secondsItem = document.getElementById("seconds");

2. Access Buttons ( start/stop and rest )

const startStop = document.getElementById("start-stop");
const reset = document.getElementById("reset");

3. Initialize Variables

let hours = 0,
  minutes = 0,
  seconds = 0;

let timer = null,
  isRunning = false;

Why These Variables?

4. Start and Stop Logic in this Stopwatch

const startOrStopWatch = () => {
  if (!isRunning) {
    timer = setInterval(() => {
      seconds++;

      if (seconds === 60) {
        seconds = 0;
        minutes++;
      }

      if (minutes === 60) {
        minutes = 0;
        hours++;
      }

      updateTime();
    }, 1000);

    startStop.textContent = "Stop";
    startStop.classList.add("stop");
    isRunning = true;
  } else {
    clearInterval(timer);
    timer = null;
    startStop.textContent = "Start";
    startStop.classList.remove("stop");
    isRunning = false;
  }
};

JavaScript Logic Explanation

5. Update time on UI ( Logic to maintain two digit format )

const updateTime = () => {
  hoursItem.textContent = hours < 10 ? "0" + hours : hours;
  minutesItem.textContent = minutes < 10 ? "0" + minutes : minutes;
  secondsItem.textContent = seconds < 10 ? "0" + seconds : seconds;
};

Why < 10 Check?

6. Reset action button logic

const resetWatch = () => {
  clearInterval(timer);
  timer = null;

  hours = 0;
  minutes = 0;
  seconds = 0;

  updateTime();

  isRunning = false;
  startStop.textContent = "Start";
  startStop.classList.remove("stop");

  alert("Reset stopwatch");
};

Reset Logic Explained

7. Add event listener to both action buttons

startStop.addEventListener("click", startOrStopWatch);
reset.addEventListener("click", resetWatch);

Live Demo and check out full code

Stopwatch using JavaScript

Conclusion

Stay Ahead in Your Tech Career 🚀

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top