Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
How can ik change the interval of setInterval while running

Iam making get requests with an interval. On page load i want an interval of 2000, but after 1 get request the interval has to be changed to 20000. What is the best approach in this case ?

$(document).ready(function(){

setInterval(function(){

    $.ajax({

        type: 'GET',
        url : "{% url "messages" object.id %}",
        success: function(response){
            console.log(response);
            $("#display").empty();
            for (var key in response.messages)

                {
                    var temp = "<div class='container manager' style='color:black'><b>" + response.messages[key].werknemer_naam ;
                    
                }

        },
        error: function(response){
            alert('An error occured')
        }
    });
},2000);
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't change the interval, all you can do is clear it and call setInterval again with the new value. But you can save a bit of code by defining the callback function separately. In your case, I'd probably do this:

function makeRequest() {
  // make the request
}

setTimeout(() => {
  makeRequest();
  setInterval(makeRequest, 20000);
}, 2000);

So it would wait 2000ms, then make the first request and set the request to run every 20000 from then on.

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Create a boolean set to false outside the setInterval block.
  2. Create another variable w/ the default interval time.
  3. When the first response comes in, set the boolean to true.
  4. With the new true value, the setInterval variable changes to a new value.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda