Thursday, March 21, 2024

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Stopwatch</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
    }

    h1 {
      margin-top: 50px;
    }

    #time {
      font-size: 3em;
      margin-top: 30px;
    }

    button {
      font-size: 1.2em;
      margin: 10px;
      padding: 10px 20px;
      border-radius: 5px;
      border: none;
      background-color: #1abc9c;
      color: #fff;
      cursor: pointer;
    }

    button:hover {
      background-color: #16a085;
    }
  </style>
</head>
<body>
  <h1>Stopwatch</h1>
  <div id="time">00:00:00</div>
  <button id="start">Start</button>
  <button id="stop">Stop</button>
  <button id="reset">Reset</button>

  <script>
    var hours = 0;
    var minutes = 0;
    var seconds = 0;
    var milliseconds = 0;
    var timer;

    function updateTimer() {
      milliseconds += 10;

      if (milliseconds == 1000) {
        milliseconds = 0;
        seconds++;
      }

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

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

      document.getElementById("time").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
    }

    function pad(num) {
      if (num < 10) {
        return "0" + num.toString();
      } else {
        return num.toString();
      }
    }

    document.getElementById("start").addEventListener("click", function() {
      timer = setInterval(updateTimer, 10);
    });

    document.getElementById("stop").addEventListener("click", function() {
      clearInterval(timer);
    });

    document.getElementById("reset").addEventListener("click", function() {
      clearInterval(timer);
      hours = 0;
      minutes = 0;
      seconds = 0;
      milliseconds = 0;
      document.getElementById("time").innerHTML = "00:00:00";
    });
  </script>
</body>
</html>

No comments:

Post a Comment

AI Story Writing Tool AI Story Writing Tool Moral Educational Historical ...