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

时间:2014-08-26 06:26:04

标签: php random

我想在每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/>";
    } 

3 个答案:

答案 0 :(得分:0)

怎么样......

    // 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);

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

<?php

while (1==1) {
    echo rand(3,10);
    sleep(5);
}