dsb0003795 2017-02-07 06:36
浏览 769
已采纳

如何在php中清除以前的输出和回显

ag.php

<?php  
ignore_user_abort(true);
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
$i=0;
while(1){
echo $i;
$i++;
ob_flush();
flush();

    if (connection_aborted()){
        break;
    }

    usleep(1000000);
}

ajax:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    console.log(xhttp.readyState+" "+xhttp.status);
  if (xhttp.readyState === 3 && xhttp.status ===200) {
      console.log(xhttp.responseText+"
");

       }
  };
  xhttp.open("GET", "ag.php", true);
  xhttp.send();

hi, at above code, i want to make a persistent connection with php and echo data in while block in 1 second interval and data comes browser like this;
0
01
012
0123
...
but i want echo data to browser like;
0
1
2
3
...
but couldn't achive it so i found this [How to clear previously echoed items in PHP about my question but not exactly what i want i think. anybody know is there any way to empty/remove previous echoed data? is it possible? help please.

  • 写回答

2条回答 默认 最新

  • duanjiao8007 2017-02-07 06:54
    关注

    use substr()

            var xhttp = new XMLHttpRequest();
            var lastResponse = '';
            xhttp.onreadystatechange = function() {
                //console.log(xhttp.readyState+" "+xhttp.status);
                if (xhttp.readyState === 3 && xhttp.status ===200) {
    
                    var currentAnswer = xhttp.responseText.substr(lastResponse.length);
                    lastResponse = xhttp.responseText;
                    console.log(currentAnswer+"
    ");
    
                }
            };
            xhttp.open("GET", "ag.php", true);
            xhttp.send();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备