【问题标题】:How to alert message once and not multiple times - JavaScript如何警告消息一次而不是多次 - JavaScript
【发布时间】:2017-05-06 18:31:24
【问题描述】:

我有一个功能

        function postItems(){
            var url = baseURL + "order_items/"
            var oucu = $("#salesPerson").val()
            for (var i = 0; i < currentItems.length; ++i) {
                $.post(url, {
                    OUCU: oucu,
                    password: password,
                    order_id: currentOrderId,
                    widget_id: currentItems[i][0],
                    number: currentItems[i][1],
                    pence_price: currentItems[i][2]

                }, function(data) {
                    var obj = $.parseJSON(data);
                    if (obj.status == "success") {
                        alert("Order has been placed");
                    } else {
                        alert(obj.message);
                    }
                })

            }
        }

现在它会在每件商品发布后提醒我已下订单。在所有项目都已发布到 API 之后,有没有办法让它只告诉我一次?如果我将警报移到回调函数之外,我将无法访问 obj.status

【问题讨论】:

  • 您是否考虑过将消息保存到循环外的列表或数组中,然后对其进行处理以生成所需的任何消息?
  • 如果您使用同步循环来处理异步操作,您将遇到一些协调问题。考虑实现一个异步循环并提供一个回调,该回调将在整个 post 操作完成时触发。
  • 您不想在 for 循环中发出 $.post 请求。只是不要。你想要的是做一个请求传递currentItems 的整个数组(我相信它是一个数组)然后在服务器端做你需要做的事情。然后您将收到一个针对一个请求的警报消息。

标签: javascript jquery iteration alert


【解决方案1】:

跟踪已处理的订单并在处理完所有订单后触发 alert():

    var ordersProcessed = [];
    function postItems(){
        var url = baseURL + "order_items/"
        var oucu = $("#salesPerson").val()
        for (var i = 0; i < currentItems.length; ++i) {
            $.post(url, {
                OUCU: oucu,
                password: password,
                order_id: currentOrderId,
                widget_id: currentItems[i][0],
                number: currentItems[i][1],
                pence_price: currentItems[i][2]

            }, function(data) {
                var obj = $.parseJSON(data);
                ordersProcessed.push(obj);
                if(ordersProcessed.length == currentItems.length) {
                    var message = "";
                    for(var x = 0; x < ordersProcessed.length; x++) {
                        if(ordersProcessed[x].status == "success")
                            message += "Order has been placed";
                        else
                            message += ordersProcessed[x].message;
                        message += "\n";
                    }
                    alert(message);                  
                }
            })

        }
    }

【讨论】:

  • 谢谢,很有帮助!
【解决方案2】:

我会做一些像这样更干净的事情。

// function to post items to the server
function postItems (options, items, done) {
    items.forEach(function (item, index, items) {
        var lastItemIndex = items.length - 1
        $.post(options.url, {
            ouch: option.ouch,
            password: option.password,
            order_id: option.currentOrderId,
            widget_id: item[0],
            number: item[1],
            pence_price: item[2]
        }, function (data) {
            if (index < lastItemIndex) return;
            return done($.parseJSON(data));
        });
    })
}

// usage
postItems({url: "/serverUrl", ouch:'someString', password: 'itsASecret'}, ["item", "collection", "goes", "here"], function(data){
    alert(data.message);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    相关资源
    最近更新 更多