Calling a Setinterval variable

Asked

Viewed 283 times

4

I have the variable X

X = setInterval(function() {  ...

and after a while I stopped in that setInterval with the function clearInterval(X)

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

1 answer

6


You can’t stop and start one over setInterval. What you can do is:

  • restart if no data needs to be re-used
  • simulate pause return route from within the function

First case:

Sets the function outside the setInterval and then starts/starts again:

function beeper(){
    // fazer algo
}

var x = setInterval(beeper, 1000);

// para parar:
clearInterval(x);

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

Next case

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

setInterval(beeper, 1000);

// para pausar:
semaforoVermelho = true;

// para recomeçar:
semaforoVermelho = false;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.