Home > Backend Development > PHP Tutorial > php for循环时间控制没秒钟输出一次解决办法

php for循环时间控制没秒钟输出一次解决办法

WBOY
Release: 2016-06-13 10:15:00
Original
1603 people have browsed it

php for循环时间控制没秒钟输出一次
ob_end_clean();
echo str_pad(' ', 256);
for ($i=10; $i>0; $i--)
{ echo $i;
flush();
sleep(5);

?>
网上找到这样一段代码,想每秒钟间隔输出!结果发现在火狐里没用,ie里却可以,坑爹啊,不要告诉我,php有浏览器差异!

------解决方案--------------------
因为 你太傻
------解决方案--------------------
花了点时间查了下这个问题,大概是这样的:默认情况下b/s结构为浏览器主动拉数据,ie浏览器在接受256个字节的数据后就开始显示信息了,火狐则会等服务端程序执行完毕才一并显示
我们可以加上一个http头让b/s结构变成服务器主动推送的的模式,这样火狐在接收到数据就开始显示,ie接收到256个字节的数据才开始显示:

PHP code
header("Content-type: multipart/x-mixed-replace;");echo str_pad(' ', 256);//如果去掉这一行,会出现和楼主代码相反的效果for($i=0;$i
                 
              
              
        
            
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template