0

(条件付きで) ループを作成しようとしていますが、すぐに結果を確認したくありません。行ごとに 1 秒間隔で結果を表示したいと考えています。

2 番目の 1 = 行 1、2 番目の 2 = 行 2 (および行 1 は引き続き表示されます)、2 番目の 3 = 行 3 (行 1、および行 2 はまだそこにあるなど..)

まず、PHP スクリプトはループ (条件として $i<=30 が必要なため 30 行) を実行し、その後、各行に 1 秒間隔で結果の表示を開始する必要があります。そうでなければ、mt_rand() は結果を混乱させます。

ループの例:

  for ($i=1; $i<=30; $i++) {
   $roll = mt_rand(1,3);     
    if ($roll=="1") {echo "1 <br />\n";}
elseif ($roll=="2") {echo "2 <br />\n";}
  else {echo "3 <br />\n"; }

jQuery や無意味に長いコードではなく、シンプルで短いスクリプトで、本当にシンプルで機能的なものを見つけようとしています。私の JS スキルは非常に低いので、皆さんに助けてもらいたいです。手伝って頂けますか ?

4

4 に答える 4

0

setTimeOut() の問題は、最初の oneNumber() 関数が実行されたときにループが終了し、y=31 になることです。setTimeOut() 関数を呼び出すときに変数値を修正する方法があると思いますが、見つけられませんでした。それが私が見つけた唯一の方法です:

<!DOCTYPE html>
    <body>
    <?php
    for ($i=1; $i<=30; $i++) {
         $roll = mt_rand(1,3);
            if ($roll==1) {echo '<b id="b'.$i.'" style="display: none;"> 1</b>';}
        elseif ($roll==2) {echo '<b id="b'.$i.'" style="display: none;"> 2</b>';}
          else {echo '<b id="b'.$i.'" style="display: none;"> 3</b>';}
                             }
    ?>
    <script>
    window.onload = function () { 

    window.setTimeout(function() {
    document.getElementById('b1').style.display="block";
    },1000)
    window.setTimeout(function() {
    document.getElementById('b2').style.display="block";
    },2000)
    window.setTimeout(function() {
    document.getElementById('b3').style.display="block";
    },3000)
    window.setTimeout(function() {
    document.getElementById('b4').style.display="block";
    },4000)
    window.setTimeout(function() {
    document.getElementById('b5').style.display="block";
    },5000)
    window.setTimeout(function() {
    document.getElementById('b6').style.display="block";
    },6000)
    window.setTimeout(function() {
    document.getElementById('b7').style.display="block";
    },7000)
    window.setTimeout(function() {
    document.getElementById('b8').style.display="block";
    },8000)
    window.setTimeout(function() {
    document.getElementById('b9').style.display="block";
    },9000)
    window.setTimeout(function() {
    document.getElementById('b10').style.display="block";
    },10000)
    window.setTimeout(function() {
    document.getElementById('b11').style.display="block";
    },11000)
    window.setTimeout(function() {
    document.getElementById('b12').style.display="block";
    },12000)
    window.setTimeout(function() {
    document.getElementById('b13').style.display="block";
    },13000)
    window.setTimeout(function() {
    document.getElementById('b14').style.display="block";
    },14000)
    window.setTimeout(function() {
    document.getElementById('b15').style.display="block";
    },15000)
    window.setTimeout(function() {
    document.getElementById('b16').style.display="block";
    },16000)
    window.setTimeout(function() {
    document.getElementById('b17').style.display="block";
    },17000)
    window.setTimeout(function() {
    document.getElementById('b18').style.display="block";
    },18000)
    window.setTimeout(function() {
    document.getElementById('b19').style.display="block";
    },19000)
    window.setTimeout(function() {
    document.getElementById('b20').style.display="block";
    },20000)
    window.setTimeout(function() {
    document.getElementById('b21').style.display="block";
    },21000)
    window.setTimeout(function() {
    document.getElementById('b22').style.display="block";
    },22000)
    window.setTimeout(function() {
    document.getElementById('b23').style.display="block";
    },23000)
    window.setTimeout(function() {
    document.getElementById('b24').style.display="block";
    },24000)
    window.setTimeout(function() {
    document.getElementById('b25').style.display="block";
    },25000)
    window.setTimeout(function() {
    document.getElementById('b26').style.display="block";
    },26000)
    window.setTimeout(function() {
    document.getElementById('b27').style.display="block";
    },27000)
    window.setTimeout(function() {
    document.getElementById('b28').style.display="block";
    },28000)
    window.setTimeout(function() {
    document.getElementById('b29').style.display="block";
    },29000)
    window.setTimeout(function() {
    document.getElementById('b30').style.display="block";
    },30000)
    }   

    </script>
    </body>
    </html>
于 2013-09-06T09:54:02.270 に答える
0

各行の後に PHP を 1 秒間一時停止し、起動するたびに出力バッファーをフラッシュできます。

for ($i=1; $i<=30; $i++) {
    $roll = mt_rand(1,3);     
    if ($roll == "1")
        echo "1 <br />\n";
    else if ($roll == "2")
        echo "2 <br />\n";
    else
        echo "3 <br />\n";

    // Flush the output buffer
    ob_flush();
    // Sleep for one second
    sleep(1);
}

PHPは出力をバッファリングするため、呼び出す必要があるob_flush()ため、単純にスリープするとページの出力が遅くなりますが、ブラウザにはすぐに表示されます。

これを行う場合、スクリプトの実行時間が PHP で許可されている最大実行時間よりも長くなる可能性があることに注意してください。コードでこの制限を設定できます。

set_time_limit(90); // Maximum execution time is 90 seconds
set_time_limit(0);  // No execution time limit

またはあなたのphp.iniファイルで:

max_execution_time = 90
于 2013-08-28T15:40:54.637 に答える