Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda