是否可以在 javascript/jquery 中暂停 for 循环?
我有一个$.each
, 穿过一个 63 长的数组,里面有一个for
,里面有另一个for
,里面还有另一个for
(使each
> for
> for
> for
),现在每个for
循环都通过数组,这使得 63^4 (等于15752961)不同的组合,这需要时间......
Sooo,是否可以在 2k 组合时暂停 2 秒?
我想暂停循环的原因是解锁 UI ......
代码:
var $arr = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"],
len = $arr.length,
l = 0,
$("body").delay(1000).show(0, function(){
$.each($arr, function(index, val){ // 63
for(var i = 0; i < len; i++){ // 3969
for(var j = 0; j < len; j++){ // 250047
for(var k = 0; k < len; k++){ // 15752961
thing = val+i+j+k;
l++;
$("#loading").text(l+"/15752961");
console.log(l+"/15752961");
$("<div>"+thing+"</div><br />").prependTo("body");
}
}
}
})
})
附言。如果有人认为,他们可以解决我的问题,请这样做:D
/* 编辑 */
如果我尝试运行这个服务器端,我会得到一个服务器错误,如果我使用foreach
或for
$smaa = range("a","z");
$store = range("A","Z");
$tal = range("0","9");
$arr = array_merge($smaa, $store, $tal);
for($i = 0; $i < $count; $i++){ // 62
for($j = 0; $j < $count; $j++){ // 3844
for($k = 0; $k < $count; $k++){ // 238328
for($l = 0; $l < $count; $l++){ // 14776336
$finish[] = $arr[$i].$arr[$j].$arr[$k].$arr[$l];
}
$finish[] = $arr[$i].$arr[$j].$arr[$k];
}
$finish[] = $arr[$i].$arr[$j];
}
$finish[] = $arr[$i];
}
foreach($arr as $first){ // 62
$finish[] = $first;
foreach($arr as $second){ // 3844
$finish[] = $first.$second;
foreach($arr as $third){ // 238328
$finish[] = $first.$second.$third;
foreach($arr as $fourth){ // 14776336
$finish[] = $first.$second.$third.$fourth;
}
}
}
}
请注意,我不会同时使用两者