简体   繁体   中英

How to alert message once and not multiple times - JavaScript

I have a function

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

            }
        }

Right now it alerts me the order has been placed after every item has been posted. Is there a way i can get it to tell me this only once, after all the items have been posted to the API? I can't access obj.status if i move the alert outside the callback function.

Keep track of processed orders and fire alert() once all orders are processed:

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

        }
    }

I would do something a lot cleaner like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM