I'm trying to make a pomodori timer in React but the function that should decrease timer when it's on isn't pausing.
This is my function:
const runTime = () =>{
let date = new Date().getTime();
let nextDate = new Date().getTime() +1000;
if(active){
let interval = setInterval(()=>{
date = new Date().getTime();
if(date>nextDate && active){
setTime((prev)=>{
return prev-1;
});
nextDate+=1000;
}
},30)
}
localStorage.clear();
localStorage.setItem('interval-id', interval);
if(!active){
clearInterval(localStorage.getItem('interval-id'));
}
}
and the variables used is this:
const [sessionTime, setSessionTime] = React.useState(25*60);
const [active, setActive] = React.useState('false');
const [time, setTime] = React.useState(sessionTime);