Home > Software design >  stop watch does not show exact time:react js
stop watch does not show exact time:react js

Time:11-22

i am trying to create a stopwatch with minitues seconds and milli seconds when i click on start i shoud start i manage to create one but the timing is not correct it takes about 5 seconds to reach 1 sec , also the after 00:60:000 it goes back to 00:00:000

const [timerState, setTimerState] = useState({});

   const intervalId = setInterval(() => {
        setCount((prev) => prev   1);
      }, 1);
  const formatTimer = () => {
    let remaining = count;

    const millis = remaining % 1000;
    remaining = Math.floor(remaining / 1000);

    const seconds = remaining % 60;

    remaining = Math.floor(remaining / 60);
    const minutes = remaining;

    return `${minutes.toString().padStart(2, "0")}:${seconds
      .toString()
      .padStart(2, "0")}:${millis.toString().padStart(3, "0")}`;
  };

<h2>{formatTimer()} </h2>

also i want to store the formatTimer when stoped into a useState so that i can pass it to a api

the working example i have created a sandbox Edit stop-watch-does-not-show-exact-timereact-js

  • Related