Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda