如何一次而不是多次提醒消息 - JavaScript

时间:2017-05-06 18:31:25

标签: javascript jquery iteration alert

我有一个功能

        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

2 个答案:

答案 0 :(得分:0)

处理完所有订单后跟踪已处理的订单和火警()

    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);                  
                }
            })

        }
    }

答案 1 :(得分:0)

我会像这样做一些更干净的东西。

def next_scene(self,scene_name):
    if scene_name == 'finished':
        print "Game Done"
        exit(1)
    return Map.scenes.get(scene_name)