I have a timer MM:SS that is counting upwards to a set time of MM:SS. i.e. from 00:00 to 45:00.
My state is current set at.
const [currentTime, setCurrentTime] = useState<CurrentTime>({
minutes: 0,
seconds: 58,
});
My useEffect timer to continiously update time is set up as...
useEffect(() => {
let interval: any;
if (!finished) {
interval = setInterval(() => {
if (currentTime.seconds === 60) {
setCurrentTime({
...currentTime,
minutes: currentTime.minutes++,
seconds: 0,
});
} else {
setCurrentTime({
...currentTime,
minutes: currentTime.minutes,
seconds: currentTime.seconds++,
});
}
}, 1000);
} else {
clearInterval(interval);
}
return () => clearInterval(interval);
}, [finished]);
The problem I have is that when the seconds reach 60. The minute property updates, but the seconds property never updates and is always stuck at 60, leading to a continuous output of 01:00, 02:00, 03:00, 04:00 05:00, ..., in the console.
What do I need to do so that when 60 seconds is reached, the minute goes up by 1 and the seconds goes back to 0 at the same time.