duanbinmi8970 2013-02-14 22:03
浏览 55
已采纳

使用flush()替换最后一行而不是在php中创建新行

Lets say my php code is similar to below. . .

$range = range(0, 5);
foreach ($range as $times) {
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<1; $i++){
echo "<br>example ".$times;
echo str_pad('',4096)."
";   
ob_flush();
flush();
sleep(1.2);
}
ob_end_flush();
}

What it displays is. . .

example1
example2
example3
example4
example5

It waits a short period of time before showing the next line, I don't want to display all five at once, I want to replace example1 with the next one and so on for all five is this possible in php? any answers welcome

  • 写回答

2条回答 默认 最新

  • duan20081202 2013-02-14 22:25
    关注

    You Would need to use javascript to replace the element already on the page:

    <p id="ob">1</p>
    <?php
    $range = range(0, 5);
    foreach ($range as $times) {
        if (ob_get_level() == 0) ob_start();
        for ($i = 0; $i<1; $i++){
            echo "<script>document.getElementById('ob').innerHTML ='example$times';</script>";
            echo str_pad('',4096)."
    ";
            ob_flush();
            flush();
            sleep(1.2);
        }
        ob_end_flush();
    }
    ?>
    

    Or a much better way would be to use AJAX to poll the server for the next value.

    <?php
    if(isset($_GET['poll'])){
        echo $_GET['poll']+1;
        die;
    }
    ?>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    var value = 0;
    function poll(){
        setTimeout(function(){
            $.ajax({ url: "./test.php?poll="+value,cache: false,
            success: function(data){
                value = data;
                $("#ob").html('example'+value);
                //Next poll
                poll();
            }});
        }, 1200);
    }
    
    $(document).ready(function(){
        poll();
    });
    </script>
    <p id="ob">example0</p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类