I have two functions within my background.js. First function starts a counter, the second clears it from local storage. The problem is the setInterval in the first function keeps on running even after the second function is triggered. How can I clear a setinterval from another function?
background.js
function startTimelapsFunction() {
if (!localStorage.getItem('startTime')) {
localStorage.setItem('startTime', Date.now());
}
const startTime = parseInt(localStorage.getItem('startTime'), 10);
const writeTime = () => {
console.log('startTime', startTime)
const currentTime = Date.now();
console.log('currentTime', currentTime)
const secondsDiff = Math.floor((currentTime - startTime) / 1000);
console.log('secondsDiff', secondsDiff)
document.body.innerText = secondsDiff;
localStorage.setItem('myCat', secondsDiff);
};
setInterval(writeTime, 1000);
writeTime();
}
function resetTimelapsFunction() {
localStorage.removeItem("myCat");
localStorage.removeItem("startTime");
}