您的位置:首页 > 编程语言 > PHP开发

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

2015-09-23 18:56 766 查看
使用定时器的时候当然想网页能够看到输出,不希望网页直接卡住,定时器结束输出一片。

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

<?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上测试,缺一不可。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: