我正在尝试使用For循环和JQuery mobile中的数组发出ajax请求。
我想:
到目前为止,我所拥有的并不真正有用(这就是我在这里的原因)。
var req1 = [];
var req2 = [];
var size = //some number passed to here//;
//Create size number of unique ajax json requests
for (i = 0; i < size; i++) {
requestA[i] = // GET request for json datatype //;
requestB[i] = $.ajax(requestA[i]);
};
for (j = 0; j < size; j++) {
requestB[j].done (function (response) {
if (response[j].results.length > 0) (
requestB[j] = response[j].results;
}
});
$(document).ajaxStop (function() {
// Do this after all ajax is done //
});
答案 0 :(得分:0)
在回调中尝试锁定数组。
像这样的东西
var isLoked = false;
// ...
if (response[j].results.length > 0 && !isLocked) (
isLocked = true;
requestB[j] = response[j].results;
isLocked = false;
}
P.S。为什么你不想要make request并在同一个循环中分配回调?
答案 1 :(得分:0)
我不知道你为什么这样做我会用$.when
来设置回调...并且可以将所有回复传递给它。
var req = [];
var size = 5;
for (i = 0; i < size; i++) {
// not sure what the signicance of A and B arrays are here...
requestA[i] = // GET request for json datatype //;
requestB[i] = $.ajax(requestA[i]);
// but we want to push them all onto req
req.push(requestA[i]);
req.push(requestB[i]);
};
// so we need a callback for when they are all finished:
jQuery.when.apply(null, req).then(successFunction, failFunction);