Home  >  Article  >  php教程  >  聊天室技术-如何在只有新发言时才刷新

聊天室技术-如何在只有新发言时才刷新

WBOY
WBOYOriginal
2016-06-13 10:24:58730browse

在主动刷新时,程序要不停的判断是否有新的发言,如果没有则重复,
这里我介绍一种类似 C 语言编程效果的方法
//一个时间标记,因为一般 php 脚本执行有时间限制
$delaytime=0;
//循环
while(1)
{
//判断是否有新的发言,我这里 $filename 存放的是发言总数,$last是上次已经显示的的发言
$message = file($filename);
$number = $message[0];
//延迟1秒钟
sleep(1);
//时间标记增加
$delaytime++;
//如果时间标记快到允许的脚本运行时间则退出循环
if($delaytime > 25) break;
//如果有新的发言则退出循环
if($number > $last) break;
}
//处理更新
... ...
这样就不会发生页面不停刷新,非常烦人的情况了!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn