I'm writing a countdown timer in React. I have two buttons: "pause-resume" and "reset" with onClick event handlers. clearInterval
doesn't work in either of them
Struggling to understand what is wrong
let timer = {
minutes: 25,
seconds: 0}
let [remainingTime, setTime] = useState(timer);
let [paused, setPaused] = useState(true);
let counting
function handleClick(e) {
const targetId = e.currentTarget.id
if (targetId === 'pause-resume') {
setTime(remainingTime = {...remainingTime, seconds: remainingTime.seconds === 0 ? 59 : remainingTime.seconds})
if (paused === true) {
setPaused(false)
counting = setInterval(() => {
setTime(remainingTime = {...remainingTime, seconds: remainingTime.seconds - 1})}, 1000)
} else {
clearInterval(counting)
setPaused(true)
}
} else if (targetId === 'reset') {
clearInterval(counting);
setTime(timer)
}
}