【问题标题】:show message while all promises are resolved在所有承诺都解决时显示消息
【发布时间】:2019-04-18 16:40:28
【问题描述】:

我有很多请求 ajax,所有这些都结束了,显示消息:“承诺结束”,但我想在“处理”或“工作......”时显示消息,我不知道。我用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);
        });
    }
});

【问题讨论】:

  • $('.delete:checked').each 之前显示“工作”消息 - 但不要使用 alert - 因为那会阻塞

标签: javascript jquery ajax promise


【解决方案1】:

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 中的适当位置

【讨论】:

  • 大概promesas.push(...)(如问题)应该是promises.push(...)
猜你喜欢
  • 2016-01-10
  • 2015-10-19
  • 2018-06-26
  • 1970-01-01
  • 2014-03-12
  • 2017-12-12
  • 2015-09-04
  • 2015-11-19
相关资源
最近更新 更多