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

122
Views
Synchronously loop through AJAX calls with DOM updates

I've seen similar questions asked, but it seems that none of them are quite the same circumstance and none of the suggestions I've tried have worked. I need to call a POST from within a for loop, and I want the loop to wait for the response to complete, because it needs the previous iteration to be done before the next one can begin. I tried setting async to false for the ajax call, but that didn't work because I need to update the DOM before the beginning of the loop, after each iteration, and finally at the end, and when the http POST is synchronous it just skips right to the ajax call and locks the thread for the response. This is apparently expected JQuery behavior so I don't think anything is wrong there. So in this code example the process runs as expected but the DOM never updates, and if the async declaration is removed then the process posts all the iterations at once and they are not processed by the server correctly.

$("#form").on("submit", function(e){    
    e.preventDefault();
    $("#submit").prop("disabled",true);
    $(".loader").css("display", "block");
    var iterations = 10;
    const baseUrl = https://exampleapi.com;
    for (let i = 0; i < iterations; i++) {
        ```set post data here```
        $.ajax({
            url: baseUrl + "/endpoint",
            async: false,
            type: "POST",
            data: JSON.stringify(data),
            contentType: "application/json",
        });     
        $(".progress").text("completed " + i + " iterations");      
    }
    $(".loader").css("display", "none");

Can someone help me understand what I'm doing wrong?

about 4 years ago · Juan Pablo Isaza
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!