循环在两个不同的阵列,两个for循环相互嵌套异步(如块和setTimeout
sourced from here),并试图用从W3Schools here(标签之一)进度条的例子。进度条使用嵌套for循环
的来源(稍微修改了 '完成' 回调)异步函数:
function loopAsync(array, fn, done, chunk, context) {
context = context || window;
chunk = chunk || 10;
var index = 0;
function doChunk() {
var cnt = chunk;
while (cnt-- && index < array.length) {
// callback called with args (value, index, array)
fn.call(context, array[index], index, array);
++index;
//console.log("index is " + index);
progressBar(index);
}
if (index < array.length) {
// set Timeout for async iteration
setTimeout(doChunk, 1);
} else {
done && done();
}
}
doChunk();
}
无论是asnychronous,这些是即使与正常for循环同样的问题:
W3School示例使用的是
setInterval
,这是不准确的,因为for循环可能已经在setInterval
之前完成处理。有两个嵌套的for循环,所以不是跟踪(例如)
i
在for (var i=0...)
,它需要跟踪的第一循环*对于精度的第二循环的进展(如不出现停止 - 尤其是因为第二个循环可能会有比第一个更大的数组长度)。
例如:
异步使用上述链接例如:
loopAsync(arr1, function (item1) {
loopAsync(arr2, function (item2) {
//Comparing or processing item1 with item2
});
}, doNext);
或者,基本上不异步循环相同的:
for (var item1 = 0; item1 < arr1.length; ++item1) {
for (var item2 = 0; item2 < arr2.length; ++item2) {
//Doing things... need to track progress of both?
}
}
- 需要是通用的,可以用于任何嵌套(或非嵌套)的for-loop歌剧灰。
这些问题应该如何解决,最好不使用jQuery?
但...你执行的代码盟友表演根本不是异步的。如果你想要一个进度条,你需要用'setTimeout()'或'setInterval()'而不是'for'来重写循环,就像你链接的代码一样。 – nnnnnn
@nnnnnn请重新阅读第一段... – user1679669
我在评论前阅读了两遍。你似乎在说“这是一些异步处理数组的代码,但我将忽略它,现在这里是一个简单的同步嵌套for循环,为什么不使用链接代码中的概念?关于进度条代码,你不需要一个单独的时间间隔,你可以从循环的相同函数中控制它。你想要一个进度条用于整体进度还是每个循环一个进度条? – nnnnnn