setTimeout function works in line with my other setTimeout function as long as I keep the window open. However, if I go to another tab/window and then come back to the timer, it will be out of sync with the other countdown.
This is the first timer:
const [isDisabled, setIsDisabled] = useState(false);
const handleOnClickUp = () => {
setIsDisabled(true);
setTimeout(() => setIsDisabled(false), 60000);}
Second Timer:
const [counter, setCounter] = useState(60);
if (isDisabled == true) {
setTimeout(() => setCounter(counter - 1), 1000);
}
if (counter == 0) {
setCounter(60);
}
Returning:
return (
<>
<div>{counter}</div>
<button
disabled={isDisabled}
onClick={handleOnClickUp}
className="bg-gray-500"
>
Up
</button>
</>
);
TLDR: How can I sync the two timers together with no problems?