Calling a SetInterval variable

4

I have the variable X

X = setInterval(function() {  ...

and after a while I gave a stop in this setInterval with the function clearInterval(X)

How do I call this variable to continue the loop after it has been "deleted"?

    
asked by anonymous 29.09.2015 / 18:05

1 answer

6

You can not stop and restart setInterval . What you can do is:

  • restart if there is no data that needs to be re-used
  • simulate pause via return from within function

First case:

Defines the function out of setInterval and then starts / resumes:

function beeper(){
    // fazer algo
}

var x = setInterval(beeper, 1000);

// para parar:
clearInterval(x);

// para recomeçar:
x = setInterval(beeper, 1000);

Segudo case

var semaforoVermelho = false;
function beeper(){
    if (semaforoVermelho) return;
    // fazer algo
}

setInterval(beeper, 1000);

// para pausar:
semaforoVermelho = true;

// para recomeçar:
semaforoVermelho = false;
    
29.09.2015 / 18:11