I have a clock(interval) component where the time starts when a user visits the webpage but when the user move to other page and comes back to the clock page then the timer starts from beginning again.
How can I keep the Clock component running in background so no matter which webpage the user visits, the timer should keep going?
Thank you in advance
CLOCK COMPONENT
function Clock(props) {
const [time, setTime] = useState(0);
const run = true;
useEffect(()=>{
if (run=== true && time < 1440){
var timerID = setInterval(() => tick(time),1000);
return function cleanup(){
clearInterval(timerID);
};
}
},[run, time]);
function tick(time){
setTime(time + 10);
console.log(time);
}
return (
<div className="clock-space">
<div className="clock">{time}</div>
</div>
);
}
App Component (that calls clock comp)
return (
<div className="box">
<Header />
<PageMenu name={this.state.page} data={this.changePageNameToDefault.bind(this)}/>
<Clock />
{renderComp}
</div>