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

266
Vistas
Event after all ajax calls are complete

I have a click event that fires off 3 ajax calls:

$("#button").click(function(e) {
    e.preventDefault();

    $.ajax(call1);
    $.ajax(call2);
    $.ajax(call3);

    some_function() //should fire off AFTER all ajax calls are complete
});

Is there a way for me to confirm all ajax calls have been fired BEFORE firing off my some_function() function?

Thanks in advance!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use $.when

Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

$.when($.ajax(call1), $.ajax(call2), $.ajax(call3))
.done(function () {
     some_function();
});

If (for some reason) you have an array of promises, then you may call this method using Function.prototype.apply():

$.when.apply($, ajaxArray)
.done(function () {
   some_function();
});
over 4 years ago · Santiago Trujillo Denunciar

0

I suggest you to use async: false and put the $.ajax inside another ajax, somelike this...

$.ajax({
    async: false,
    // ...
    complete: function() {
        $.ajax({ // second ajax
            async: false,
            // ...
            complete: function() {
                $.ajax({ // second ajax
                    async: false,
                    // ...
                    complete: function() {
                         some_function();
                    }
                });
            }
        });
    }
});
over 4 years ago · Santiago Trujillo Denunciar

0

It depends if you require success responses, but consider the following:

$.ajax({
    url: "http://some.url.example.com/endpoint",
})
.always(function (data){
    // Process the data
    $.ajax({
        url: "http://some.url.example.com/endpoint2",
    })
    .always(function (data2){
        // Process the data
        $.ajax({
            url: "http://some.url.example.com/endpoint3",
        })
        .always(function (data) {
            someFunction();
        });
    });
});

There is a lot more reading that could be done about jQuery deferred objects and ES6 Promises.

If you wish to set a timeout and don't care about the result, set the timeout option `$.ajax({url: "http://some.url.example.com/endpoint", timeout: 500})

OR

Set a variable in each and use window.setTimeout to check when they've all been set, but that is horrible.

over 4 years ago · Santiago Trujillo 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