在所有承诺都得到解决的同时显示消息

时间:2019-04-17 23:03:39

标签: javascript jquery ajax promise

我有很多请求ajax,所有这些都结束了,显示消息:“ promises end”,但是我想在“ Processing”或“ Working ...”时显示消息,但我不知道。我用的是jQuery

$("#btnDelete").click(function () {
    var promises = [];
    if (confirm("¿Seguro que quiere borrar?")) {
        $('.delete:checked').each(function (index, value) {
            promesas.push(
                $.ajax({
                    method: "POST",
                    url: "transaccion.php",
                    data: {
                        id_pagos_detalle: $(this).val(),
                        accion: "Registrar_inf_consignacion",
                        estado_elemento: "Si"
                    }
                }).then(function (msg) {
                    if(msg.indexOf("SUCCESS") != -1){
                        console.log("delete this element");
                    }else{
                        alert("ERROR");
                        return false;
                    }
                })
            );  // end promise  
        });

        // process all promises
        $.when.apply($, promises).then(function(){
            alert("Promises ended");
            location.reload(true);
        });
    }
});

1 个答案:

答案 0 :(得分:0)

Jaromanda X是正确的。您可能可以执行以下操作:

$("#btnDelete").click(function () {
var promises = [];
if (confirm("¿Seguro que quiere borrar?")) {

   //Loading message here

    $('#you_div').html('Loading...');
    $('.delete:checked').each(function (index, value) {
        promesas.push(
            $.ajax({
                method: "POST",
                url: "transaccion.php",
                data: {
                    id_pagos_detalle: $(this).val(),
                    accion: "Registrar_inf_consignacion",
                    estado_elemento: "Si"
                }
            }).then(function (msg) {
                if(msg.indexOf("SUCCESS") != -1){

                      //hide the loading message
                      $('#you_div').hide();

                    console.log("delete this element");
                }else{
                    alert("ERROR");
                    return false;
                }
            })
        );  // end promise  
    });

    // process all promises
    $.when.apply($, promises).then(function(){



        alert("Promises ended");
        location.reload(true);
    });
}
});

请记住将you_div添加到html中的适当位置