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

223
Vistas
jquery handling recursive ajax calls, how to tell the browser it's done

I have an endpoint that returns me a next token if there are more things to fetch, and the code that I'm writing to fetch it looks like so:

        function get_all_entities(campaign_id, page){
            get_campaign_entities(campaign['id'], page)
                .success(function(data){
                    if (data['next']){
                        console.log(data['next'])
                        get_all_entities(campaign_id, page+1)
                    }
                })
        }

        get_all_entities(campaign['id'], 1)

Now, while this works, the function that actually calls it get_all_entities(campaign['id'], 1) has no way on knowing when everything is done. I've tried adding a while loop like so but it crashes my browser (everything freezes up)

        function get_all_entities(campaign_id, page){
            all_done = false
            get_campaign_entities(campaign['id'], page)
                .success(function(data){
                    if (data['next']){
                        console.log(data['next'])
                        get_all_entities(campaign_id, page+1)
                    }
                    else{
                        all_done = true
                    }
                })
            while (!all_done){
                setTimeout(function(){
                }, 2000);
            }
            return all_done
        }

I've also tried returning get_campaign_entities and use the done callback but the done callback only works for the first call.

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

0

You could add a callback argument:

function get_all_entities(campaign_id, page, callback) {
  get_campaign_entities(campaign['id'], page)
    .success(function(data) {
      if (data['next']) {
        console.log(data['next'])
        get_all_entities(campaign_id, page + 1, callback)
      } else {
        callback();
      }
    })
}

get_all_entities(campaign['id'], 1, () => {
  console.log('Done!');
});
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