Reputation: 75
I'm building a chrome addon in manifest v2. I have an interval that runs every minute. Inside it, I have a function that supposed to reload the page will reload every 3 minutes if a boolean is false. If it is true, the timer of the 3 minutes need to reset itself.
For some reason, when I'm setting the timer and logging it, it is stuck at '1'. It will move only when I will consantly set it again - which by theory should reset it but nope - it just continues.
When I call for clearTimeout, nothing happens - the timer stays the same. Even if I try to equel the timer variable into a null, it's still continues regularly.
This is the code, hope that you could help me figure this out:
var timerLoop = [].
var audioPlayed = false;
var timer = null;
function clearTimerLoop(){
for ( var i = 0; i < timerLoop.length; ++i ){
clearTimeout( timerLoop[i] );
timerLoop[i] = null; //Tried here to make it a null, no success
}
timerLoop = []; //Tried here to remove everything from the array, no success
console.log("Timer cleared");
}
function startTimer(){
timer = window.setTimeout(refreshPage, 180000); /* reload every 3 minutes */
timerLoop.push(timer);
console.log("Timer created");
}
loopInterval = setInterval(function(){loopChecker();}, 60000);
function loopChecker(){
if(audioPlayed){
clearTimerLoop()
timer = null;
audioPlayed = false;
}
if(!audioPlayed && timer == null){
startTimer();
console.log("Refresh timer running (" + timerLoop[0] + ")");
}
console.log(timer);
}
Thanks in advance.
As you can see I am resetting the timer as much as I can but nothing happens. In addition, the timer is stuck on 1 unless I set it again in every timer loopChecker() runs, and again - this should reset it but it continues. I thought maybe every run sets a new timer so I created an array that pushes the timer each time it's created and then when I want to clear, every timer is cleared. Didn't help.
Upvotes: 0
Views: 152