我遇到以下代码的问题:
jQuery.each正在加速运行,无需等待JSON请求完成。因此,'thisVariationID'和'thisOrderID'变量会被循环的最新迭代重置,然后才能用于较慢的getJSON函数。
有没有办法让每个迭代的.each等到getJSON请求和回调函数完成后再继续下一次迭代?
$.each($('.checkStatus'), function(){
thisVariationID = $(this).attr('data-id');
thisOrderID = $(this).attr('id');
$.getJSON(jsonURL+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&callback=?', function(data){
if (data.response = 'success'){
//show the tick. allow the booking to go through
$('#loadingSML'+thisVariationID).hide();
$('#tick'+thisVariationID).show();
}else{
//show the cross. Do not allow the booking to be made
$('#loadingSML'+thisVariationID).hide();
$('#cross'+thisVariationID).hide();
$('#unableToReserveError').slideDown();
//disable the form
$('#OrderForm_OrderForm input').attr('disabled','disabled');
}
})
})
看看这里: [http://stackoverflow.com/questions/1761417/jQuery的可能对等待换得到完成装载前方的持续] [1] [1]:http://stackoverflow.com/questions/1761417/ jquery可能要等待完成加载之前继续 –