如何组合每一个然后

时间:2013-03-05 14:57:12

标签: jquery ajax soap locking

我正在尝试在.each()内创建soap请求,但jQuery在第一个soap请求完成之前执行下一个对象(在列表中)。我怎么能锁上它?

例如:

$(xmlHttpRequest.responseXML).find("Items").find("PowerPlant").each(function(index, item){      
    sendSOAPRequest(item);
});

2 个答案:

答案 0 :(得分:2)

我不是100%确定您使用的是哪个库,但在某些版本的某些sendSOAPRequest调用中,您应该能够将async变量设置为false,从而强制它们一次执行一次:

$(xmlHttpRequest.responseXML).find("Items").find("PowerPlant").each(function(index, item){      
    sendSOAPRequest(item, {async: false}); //Might be other syntax. Look at the doc for your library.
});

如果这不起作用,你可以像Brad M建议的那样:

items = $(xmlHttpRequest.responseXML).find("Items").find("PowerPlant");           

function sendSoapReq(itemList){
    if(itemList.length > 0)
    {
        sendSOAPRequest(itemList.splice(0,1), function(){
            sendSoapReq(itemList);
        });
    }
}

sendSoapReq(items);

答案 1 :(得分:0)

您需要在soap请求的完整回调函数中执行$ .each循环的下一次迭代。