Setinterval and reset

Asked

Viewed 67 times

1

I have a setInterval with an interval of 2 seconds. I need to stop this interval at a certain point and start it again with a new time.

Scope
I do an AJAX inside the setInterval. Just when the AJAX response is Success (for example), it repeats the interval again. If it is error, it stops the interval, changes the interval time, and restarts it again.

I managed to do the following until the moment:

var TempoRequest = 2200;
var Ativo = true;
if(Ativo === true){
    VerificaChat = setInterval( function(){
        if(acao){

        //repete o intervalo dnv

        }else{
            var Ativo = false;
            var TempoRequest = 15000;
            clearInterval(VerificaChat);
        }

    }, TempoRequest );
}
  • First I need to know at what point do you want it to stop and at what point a new time is added? It gets very vague that way you asked, can I stop with click or pass mouse, how do you want to stop setinterval and how do you want to restart it with new time? What are you thinking?

  • It’s like I do an AJAX inside that setInterval. Just when the AJAX date response is Success (for example), it repeats the interval again. If it is error, it stops the interval, changes the interval time, and restarts it again. Got it?

1 answer

2

I made something very simple here, see if it helps you.

  tempo = 2000; //crio variável global para controlar o tempo,
  number = 1; //essa variavel e apenas pra imprimir e controlar o incremento, mas no seu caso vai ser controlado pelo sucesso do AJAX

  setInterval(function(){
    if (number>0 && number<5) {
      document.write(number);
      number++;
    }else{
      document.write("-")
      number = 1;
      tempo += 100; //nessa parte eu apenas incrementei o tempo da variavel, mas se vc quiser pode resetar ela ou criar modo dinâmico para setar um tempo.
    }
  },tempo);

Browser other questions tagged

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