I have setInterval function where a timer will be running.
Issue:
For example if we have 2 menus, one is home and 2nd is timer(where our timer is running), if I stay in timer page, SetInterval timer is working fine, but if I visit home page, the timer is starting where I left the page.
Requirement:
Even if I visit the Home page, the timer should not stop, it should go continuesly.
Code:
const pad = (val) => {
return val > 9 ? val : '0' + val;
};
startTimer = () => {
let timer = setInterval(function() {
document.getElementById(`seconds-${accountId}`)
? (document.getElementById(`seconds-${accountId}`).innerHTML = pad(++sec % 60))
: null;
document.getElementById(`minutes-${accountId}`)
? (document.getElementById(`minutes-${accountId}`).innerHTML = pad(parseInt(sec / 60, 10)))
: null;
}, 1000);
}
componentDidUpdate(){
this.startTimer();
}