PHP 定时器 边输出边刷新网页

使用定时器的时候当然想网页能够看到输出,不希望网页直接卡住,定时器结束输出一片。

要做到定时器不卡住输出,只需要两个函数就行了,看下面代码

<?php
//定时器测试代码 demo
//跟踪定时程序 timerPro.php
ignore_user_abort(true); 
set_time_limit(600);
$interval = 10;
$stop = 1;
    
do {
   if ($stop == 10)
      break;
    $curTime = date('y-m-d H:i:s', time());
    file_put_contents('timer.log', ' Current Time: '.$curTime.' Stop: '.$stop.PHP_EOL, FILE_APPEND);
    echo $stop."<br>";
    $stop++;
     ob_flush(); 
    flush(); 
    sleep($interval);
    
  } while(true);
?>
 ob_flush(); 
    flush(); 
需要上面两个函数网页就不会卡住了,在windows chrome上测试,缺一不可。

 

原文地址:https://www.cnblogs.com/yuzhould/p/4833242.html