如果您的程序逻辑合适,您可以通过进行部分处理并使用 setTimeout() 函数来模仿 javascript 中的异步行为,如下所示:
var result1 = []
var result1Ready = false;
var result2 = []
var result2Ready = false;
func1 = function(list, start, end) {
if(start>=end) {
result1Ready = true;
}
else {
partialEnd = start+10>end ? end : start+10;
for(i=start;i<partialEnd;i++) {
//process 10 items
//append results to array result1
}
//schedule second partial process
setTimeout(function () { func1(list, partialEnd, end); }, 50);
}
};
func2 = function(list, start, end) {
//similar to func1...
};
waitResults = function() {
if(result1Ready && result2Ready) {
func3();
}
else {
setTimeout(function () { waitResults(); }, 50);
}
};
setTimeout(function () { func1(someList, 0, listLength); }, 5);
setTimeout(function () { func2(someOtherList, 0, otherListLength); }, 5);
setTimeout(function () { waitResults(); }, 10);