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?