I have a function like this that may run for 20 minutes:
我有这样的功能,可能会运行20分钟:
function test($videoarray)
{
for($i;$i<100;$i++)
if(converVideo($videoarray[$i]))
{
echo 'video number '.$i.' has been convert';
}
else
{
echo 'video number '.$i.' Is too large.';
}
}
During this time I wish I could see the messages live, for example with Ajax/PHP.
在此期间,我希望能够看到消息,例如使用Ajax / PHP。
Is it possible?
可能吗?
Desired result:
1st min: video number 1 has been converted.
2nd min: video number 2 is too large.
,
.
.
1 个解决方案
#1
I would recommend adding flush();
after each echo
我建议添加flush();每次回声后
This should force the message to the screen once written.
这应该在写入后强制将消息发送到屏幕。
Example
echo 'video number '.$i.' has been convert';
flush();
This might not always work therefore one can always persist the messages to a database and have a polling mechanism to read the message every couple of seconds.
这可能并不总是有效,因此可以始终将消息持久保存到数据库并具有轮询机制以每隔几秒读取消息。
PHP轮询的示例
#1
I would recommend adding flush();
after each echo
我建议添加flush();每次回声后
This should force the message to the screen once written.
这应该在写入后强制将消息发送到屏幕。
Example
echo 'video number '.$i.' has been convert';
flush();
This might not always work therefore one can always persist the messages to a database and have a polling mechanism to read the message every couple of seconds.
这可能并不总是有效,因此可以始终将消息持久保存到数据库并具有轮询机制以每隔几秒读取消息。
PHP轮询的示例