2016-12-22 4 views
1

現在、私は5秒ごとに計算結果をループする必要があります。 HTML、PHPまたはJavaScriptにコードがありますか?私は新しい行で作成するのではなく、1か所で更新結果を表示できます。ループの結果を一箇所に表示

<?php 
set_time_limit(0); 
ob_implicit_flush(true); 
ob_end_flush(); 

$total=0; 
for ($i=1; $i<980; $i++) { 

$total=$total+1; 
    echo "The current number is".$total; 
    echo '<br>'; 
    sleep(5); 

} 

?> 
+0

: - ' ' –

答えて

0

      setInterval(function(){phpJavascriptClock();},2000); 
 
\t \t \t function phpJavascriptClock() 
 
\t \t \t { 
 
       var total=0; 
 
for (var i=1; i<980; i++) { 
 

 
total=total+1; 
 
    
 

 
}    
 
\t \t \t \t document.getElementById("time_update_val").innerHTML= total ; 
 
\t \t \t }
<span id="time_update_val" class="notranslate">1</span>

+0

上位コードを使用して、2秒ごとに結果を更新しました。あなたの要件に応じてそれを変更することができます –

0

「エコー」の部分は、「for」ループの外にする必要があります。以下は、私はPHPで働いているコードの例です。 ?

<?php 

    set_time_limit(0); 
    ob_implicit_flush(true); 
    ob_end_flush(); 
    $total=0; 

    for ($i=1; $i<980; $i++) { 
    $total=$total+1; 
    sleep(5); 
    } 

    echo "The current number is".$total; 

?> 
+0

私はこれが動作するとは思わない。そのコードは、ループ終了後に単一の結果しか表示しません。私が見たいのは、タイムクロックのように動く結果の数です。 – antoraya

0

使用jqueryの容易

var loopCounter = 0; 
 
(function addCount() 
 
{ 
 
    setTimeout(function() { 
 
    if (loopCounter++ < 949) 
 
    {           
 
     $('#result').html(loopCounter); 
 
     addCount(); 
 
    } 
 
}, 500); 
 
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p id='result'>start</p>