我想在循环中调用 jQuery 延迟函数,但每次迭代都应该等待上一次迭代使用延迟函数完成when()
function (num_of_iterations) {
var arr = [];
for (var i = 1; i < num_of_iterations; ++i) {
arr.push($.getJSON( ... 1 ... ));
arr.push($.getJSON( ... 2 ... ));
...
$.when.apply($, arr).then(function() {
// somehow do the next iter. only now that all the asynch getJSON's are done
});
}
return;
}
现在当然,因为getJSON
是异步的,所以所有迭代中的所有请求实际上都将在when
调用任何 s 之前发送。
我意识到我可以通过调用一个函数来使用递归来实现这一点,该函数将我在then
.
但我想知道是否有一些我缺少使用而不是递归的技术。我总是担心将来某个时候递归会用完堆栈。该参数num_of_iterations
可能非常大。
我可以用pipe()
这个吗?我在讨论它的所有关于过滤的文档时遇到了很多麻烦......