Synchronous. with asynchronous .ajax

I rack my brains by reading all the other questions on this, but cannot find the best option for my code. Basically, I have an iterator .eachthat performs an asynchronous ajax function. I need this to be synchronous without blocking browsers, so I decided not to use it async: false.

Here is the code:

$(".btn-timbrar").each(function(i, obj){
    $.ajax({
        type: 'POST',
        url: "Home?opt=Recibo_G", 
        data: {id: $(obj).data("id"), p: pago},
        success: function(response){
            progress = progress + interval;
            $barra.find("#progreso-global").width(progress);
            $barra.find("#progreso-global").html(progress+"%"); 
            if(response.indexOf("timbrado con exito") != -1){               
                $(obj).parent().html('<a title="PDF" class="btn btn-recibo-info btn-mini" target="_blank" href="/ReciboVital/Home?opt=PDF&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-file"></span> PDF</a> <a title="XML" class="btn btn-recibo-info btn-mini" target="_blank" href="/ReciboVital/Home?opt=XML&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-download-alt"></span> XML</a> <a title="Enviar Correo" class="btn btn-recibo-info btn-mini btn-correo" href="/ReciboVital/Home?opt=Correo&='+$(obj).data("id")+'"><span class="glyphicon glyphicon-envelope"></span></a> <a title="Cancelar" class="btn btn-recibo-info btn-mini btn-correo" href="/ReciboVital/Home?opt=Cancelar&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-remove"></span></a>');
                $("#example thead th:eq(5)").width(150);
            }
        }
    });
});
+4
source share
1 answer

The only way to do this without using async: falseis to use callbacks instead of a loop .each(). For example:

var buttons = $(".btn-timbrar");

(function nextButton() {
    var obj = [].pop.call(buttons);
    $.ajax({
        type: 'POST',
        url: "Home?opt=Recibo_G", 
        data: {id: $(obj).data("id"), p: pago},
        success: function(response){
            progress = progress + interval;
            $barra.find("#progreso-global").width(progress);
            $barra.find("#progreso-global").html(progress+"%"); 
            if(response.indexOf("timbrado con exito") != -1){               
                $(obj).parent().html('<a title="PDF" class="btn btn-recibo-info btn-mini" target="_blank" href="/ReciboVital/Home?opt=PDF&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-file"></span> PDF</a> <a title="XML" class="btn btn-recibo-info btn-mini" target="_blank" href="/ReciboVital/Home?opt=XML&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-download-alt"></span> XML</a> <a title="Enviar Correo" class="btn btn-recibo-info btn-mini btn-correo" href="/ReciboVital/Home?opt=Correo&='+$(obj).data("id")+'"><span class="glyphicon glyphicon-envelope"></span></a> <a title="Cancelar" class="btn btn-recibo-info btn-mini btn-correo" href="/ReciboVital/Home?opt=Cancelar&id='+$(obj).data("id")+'"><span class="glyphicon glyphicon-remove"></span></a>');
                $("#example thead th:eq(5)").width(150);
            }
            if(buttons.length > 0) { 
                nextButton();
            }
        }
    });
})();
0
source

Source: https://habr.com/ru/post/1528978/


All Articles