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

169
Views
2nd ajax request should not wait until first one completes it's execution

I am triggering two ajax request at same time. First one will take around a 20-30 seconds to complete it's job (i.e. save various large data into diff table, copy images etc.)

So 2nd ajax request will trigger when 1st one starts and will end as soon as 1st one will complete it's job. 2nd one will periodically run (in recursive loop until 1st one complete it's job) and display progress to end user.

below is the sample code to replicate the behavior:

a.php

<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
  
<input type="button" id="a" value = "click" />
<script>
$(document).ready(function() {
    var i = 1;
    var totalCall = 10;
    $("#a").click(function(){
        $.ajax({
          url: "b.php",
          data: {
            zipcode: 97201
          },
          async: true,
          success: function( result ) {
            console.log(result);
          },          
          beforeSend: function () {
              i = 1;
          },
        });
        
        
        checkProgress();
    });
    
    
    function checkProgress(){
            $.ajax({
              url: "c.php",
              data: {
                zipcode: 97201
              },
              //async: true,
              success: function( result ) {
                console.log(result);
                i = i + 1;
                if (i <= totalCall) {
                    checkProgress();
                }
              }
            }); 
        }
});
</script>

b.php

<?php
session_start();    
sleep(2);
$_SESSION["progress"] = 1;
sleep(2);
$_SESSION["progress"] = $_SESSION["progress"] + 1;
sleep(2);
$_SESSION["progress"] = $_SESSION["progress"] + 1;
sleep(2);
?>

c.php

<?php
session_start();    
echo json_encode($_SESSION['progress']);
?>

Above code does trigger two ajax request but c.php one hanging until b.php complete it's job. It does it's job only after b.php finish, you can check below screenshot. So how to solve this? enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

jquery have implemented

 $.ajax(url)
  .progress(function(){
    /* do some actions */
  })
  .progressUpload(function(){
    /* do something on uploading */
  });

Maybe it is what u are looking for ?

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!