在PHP中,每5秒自动生成一个随机值


Generate a random value automatically after each 5 sec in PHP

我想在每5秒后生成一个随机函数,并打印它。但我做不到。我的代码片段是:

    $time = date("m/d/Y h:i:s a", time());
    echo $time;
    echo "<br/>";
    while (($time+5) < date("m/d/Y h:i:s a", time()))
    {
        $time = date("m/d/Y h:i:s a", time());
        echo $time;
        echo "<br/>";
        echo rand(3,10);
        echo "<br/>";
    } 

这个怎么样。。

    // current time
    echo date('m/d/Y h:i:s a') . "'n";
    // sleep for 5 seconds
    sleep(5);
    // wake up !
    echo date('m/d/Y h:i:s a') . "'n";
    echo rand(3,10);

PHP中的

$count_for_print = 10;
for($i=1; $i<=$count_for_print; $i++)
{
    echo $i." : ".date('d-M-Y g:i:s A')." : ".rand(3,10)."<br/>";
}

JS中的

<script>
window.setInterval(function(){
  do_task();
}, 5000);

function do_task()
{
    var rand_num = get_rand(3, 10);
    var html = "<br/>";
    html += rand_num;
    var output_div = document.getElementById('output_div');
    output_div.HTML = output_div.HTML+html;
}
function get_rand(min, max)
{
    return Math.floor(Math.random() * (max - min + 1)) + min; 
}
</script>
<div  id="output_div">
</div>
<?php
while (1==1) {
    echo rand(3,10);
    sleep(5);
}