I want to create a reverse timer on button click.
I created a setInterval with a function that should take into account the data of seconds and minutes, and when you click on the start button, seconds = 59 and decrease by 1 every second, after that, when seconds = 0, minutes should decrease by 1, and seconds are 59. But after pressing the button, seconds = 59 and minutes -1, but no countdown occurs
const [time, setTime] = useState(10);
const [sec, setSec] = useState(0);
const [min, setMin] = useState(10);
let timerInt;
const timer=()=> {
timerInt = setInterval(timerF, 1000);
function timerF() {
if(sec===0){
setSec(59);
setMin(min - 1);
}
if (sec <= 0 && min <= 0) {
clearInterval(timerInt);
setSec(0);
setMin(0);
console.log('finish')
}
setSec(prev=>prev-1)
}
}
const handleStart = (time) => {
setMin(time)
setTime(time)
timer();
setDisabledBtn(true);
};
<div className="time">
<div className="time-min">{min}</div>
<span>:</span>
<div className="time-sec">{sec}</div>
</div>
<button
className="start-btn active-btn"
type="button"
disabled={disabledBtn && 'disabled'}
onClick={() => handleStart(10)}
>
START
</button>