是否可以在javascript / jquery中暂停for循环?
我有一个$.each
,它贯穿一个63长的数组,里面有一个for
,其中有另一个for
,其中还有另一个for
(使each
> for
> for
> for
),现在每个for
循环通过数组,这使得63 ^ 4(等于15752961)不同的组合,这需要时间......
Sooo,是否有可能暂停2秒,2k组合?
我想暂停循环的原因是解锁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;
}
}
}
}
请注意,我不同时使用两者
答案 0 :(得分:1)
不,没有可能&#34;暂停&#34;一个for循环,无论如何它都不会让它更快。
请勿在此处使用,但请查看window.setTimeout(fn, delay)
答案 1 :(得分:1)
没有
但是,您可以使用以下内容:
(function() {
var i=0, j=0, k=0, timer;
timer = setInterval(function() {
// loop body here
k++;
if( k >= max_k) {
k = 0;
j++;
if( j >= max_j) {
j = 0;
i++;
if( i >= max_i) {
clearInterval(timer);
}
}
}
},1);
})();
我不确定如何将其与$.each
集成,您可能必须使用纯JavaScript。但是这段代码基本上会在重绘时尽可能快地运行循环。
答案 2 :(得分:1)
这是未经测试的,但希望你能得到这个想法。说实话,这可能是你应该在服务器端处理,或者做一次并将输出存储在代码中。
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,
paused = false,
writer = '',
pauseAt = 2000;
$("body").delay(1000).show(0, function(){
function killForTwoSeconds(){ //function to timeout loop
paused = true; //set to true to kill loop
$('body').append(writer); //write to body only on pause (fewer writes to dom = faster script)
setTimeout(function(){ // wait for 2 seconds
paused = false;
loopIt(); // re-run loop
},2000);
}
function loopIt(){
$.each($arr, function(index, val){
for(var i = l; i < len; i++){
for(var j = l; j < len; j++){ //loops now start at l instead of 0 so when loop restarts it restarts at the right place
for(var k = l; k < len; k++){
if(l % pauseAt === 0){ // if l is divisible by 2000 kill
killForTwoSeconds();
}
if(!paused){ //if not paused add item to writer
thing = val+i+j+k;
l++;
$("#loading").text(l+"/15752961");
console.log(l+"/15752961");
writer = "<div>"+thing+"</div><br />";
}else if(index === len-1 && k === len-1){
$('body').append(writer); //final write
}else{
return false; // if paused return false
}
}
}
}
});
}
});