Question:
I have variable X
X = setInterval(function() { ...
and after a while I stopped this setInterval with the clearInterval(X)
How do I call this variable so that it continues the loop after I have "erased" it?
Answer:
You cannot stop and restart a setInterval
. What you can do is:
- restart if there is no data that needs to be reused
- simulate pause via return from within the function
First case:
Sets the function outside of setInterval
and then starts/restarts:
function beeper(){
// fazer algo
}
var x = setInterval(beeper, 1000);
// para parar:
clearInterval(x);
// para recomeçar:
x = setInterval(beeper, 1000);
Second case
var semaforoVermelho = false;
function beeper(){
if (semaforoVermelho) return;
// fazer algo
}
setInterval(beeper, 1000);
// para pausar:
semaforoVermelho = true;
// para recomeçar:
semaforoVermelho = false;