Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
jquery ajax diferido

He revisado todos los temas sobre esto, creo.

Tengo una función que usa jquery para actualizar un registro a través de ajax. Necesito cambiarlo para hacerlos y esperar a que termine cada uno.

He estado leyendo sobre $.Deferred y he intentado implementarlo, pero el código aún no espera. Entiendo que ajax es asíncrono y no quiero cambiar eso como tal, pero quiero que espere, ya que el código genera mensajes y los necesito en orden.

Mi código es como tal:

 //do the export: function doExport(id) { var exportComplete = $.Deferred(); var exportPromise = $.ajax({ type: "POST", url: "import.php", dataType: "json", data: { id: id } }); exportPromise.done(function(msg) { //msg contains the text from the import exportComplete.resolve(msg); }); return exportComplete.promise(); } //export all ticked items (from a list) function importInvoices() { var checked = new Array; $('#apiCall').html(""); //put all checked items into an array (id corresponds to a PK in the db) $("input[name=selectedRow]:checked").each(function(num,row) { checked.push($(row).data('id')); }); //loop through each checked item $(checked).map(function(num, id) { console.log("starting " + id) ; var prom = doExport(id).then(function(result) { console.log("done " + id); $('#apiCall').html($("#apiCall").html() + result.messages); }); }); $("#pleaseWaitContainer").hide(); }

Debe estar muy cerca, pero creo que falta algo o está en el lugar equivocado. El texto de la exportación resultante aparece donde debería, pero si (por ejemplo) selecciono 3 facturas, obtengo el texto de la consola "iniciando xxx" 3 veces y luego "terminado xxx" cuatro veces.

Como esto:

 starting 243 starting 663 starting 823 done 243 done 663 done 823

Mientras que lo que quiero es

 starting 243 done 243 starting 663 done 663 starting 823 done 823

Cualquier consejo sería apreciado con cariño ... Me estoy arrancando el pelo limitado.

cris

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Llamada exitosa

 const checked = $("input[name=selectedRow]:checked").map(function() { return $(this).data('id')) }).get() const load = function() { $.ajax({ url: `/someprocess.php?id=${checked.shift()}&otherparm=bla`, success: function() { if (checked.length > 0) load() } }) } if (checked.length > 0) load(); // start
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!