Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!