次の繰り返しに進む前に関数を実行する必要があるカスタムループがあります。これはコードです:jQuery .when()がループ内で無視されています
function customIteration(arr, i)
{
if (i==arr.length) return;
var message = arr[i];
jQuery('#stepTwo #num1StepTwo').html('Expires: ' + $num1);
jQuery('#stepTwo #num2StepTwo').html(jQuery(message).find('.num2').text());
jQuery('#stepTwo #num3StepTwo').html(jQuery(message).find('.num3').text());
i++;
jQuery.when(mySpecialFunction()).then(customIteration(arr, i));
}
mySpecialFunction():
function mySpecialFunction(){
return jQuery.ajax({
url: "https://api.site.com/customurl",
dataType: "jsonp",
data: { data1: $data1, data2: $data2 },
success: function (data) {
...some code...
},
error: function (e) {
...some other code...
}
});
}
問題は、私は、URLを取得するために待機することなく、上記のループのすべてのインスタンスですぐにヒットされていることフィドラーで見ていますmySpecialFunction()内のajaxコードからの応答。もちろん、これは私が得なければならない結果を混乱させます。
'.then(customIteration(arr、i))'を '.then(function(){customIteration(arr、i)})に変更します。 – A1rPun
@ A1rPunこれは実際に働いた!どうもありがとうございます。 – user2413079