0

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

DS9
  • 2,995
  • 4
  • 52
  • 102
  • Do you have to executed both ajax at the same time? Can't you execute second one from "success" of the first one? – vanowm Sep 10 '21 at 10:34
  • no, as i said in question, i want to display progress of 1st one in 2nd ajax request. As 1st will take time to complete it's job. so 2nd will run and show progress of 1st one. – DS9 Sep 10 '21 at 10:39
  • Since both your php files are checking the same variable, you can exit second php when that variable reached needed level... – vanowm Sep 10 '21 at 10:53

1 Answers1

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 ?

Jordi Jordi
  • 461
  • 3
  • 10
  • can you pls share any documentation link? – DS9 Sep 10 '21 at 10:57
  • you can use too The response progress (XmlHttpRequest.onprogress) https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget as indicate in https://stackoverflow.com/a/20984502/6503034 – Jordi Jordi Sep 10 '21 at 11:57