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条)

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题