0

javascript/jqueryでforループを一時停止することは可能ですか?

私は$.each、63 の長い配列を実行する を持っています。これには、for内部に別forの があり、さらに別のforがあります (Makes each> for> for> for)。forこれで、配列内の各ループが作成され、これにより 63^4 (Equals 15752961) さまざまな組み合わせで、時間がかかります...

そうそう、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");                                  
                }
            }
        }
    })
})

Ps。誰かが私の質問を解決できると思うなら、そうしてください:D

/* 編集 */ このサーバー側を実行しようとすると、サーバー エラーが発生しますforeachfor

$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;
            }
        }
    }
}

両方を同時に使用しないことに注意してください

4

3 に答える 3

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で統合する必要があるかもしれません。ただし、このコードは基本的に、再描画しながら可能な限り高速にループを実行します。

于 2012-05-31T22:36:54.443 に答える
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
                        }
                    }
                }
            }
        });
    }
});
于 2012-05-31T23:32:09.297 に答える
1

いいえ、for ループを「一時停止」することはできません。

ここでは使用しませんが、参照してください。window.setTimeout(fn, delay)

于 2012-05-31T22:34:25.880 に答える