使用PHP输出Bufferering在处理过程中发出JavaScript [英] Using PHP Output Bufferering to Issue JavaScript During Processing

查看:107
本文介绍了使用PHP输出Bufferering在处理过程中发出JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些PHP代码正在接收和处理大图像。在处理图像以使用jQuery更新DOM时,我想在某些点回应一些JavaScript。这是一些示例代码,但它不起作用。它只等待整整5秒钟,然后让警报重复发生。我希望它立即进行第一次警报,然后在5秒后进行下一次警报。

I have some PHP code that is receiving and processing large images. I'd like to echo out some JavaScript at certain points while the image is being processed to update the DOM with jQuery. Here is some sample code, but it isn't working. It just waits the entire 5 seconds and then makes the alerts happen back to back. I want it to do the first alert immediately and then next alert after 5 seconds.

ob_start();

echo '<script type="text/javascript">alert(\'1...\');</script>';

ob_flush();

sleep(5);

ob_start();

echo '<script type="text/javascript">alert(\'2...\');</script>';

ob_flush();

有人可以帮忙吗?

推荐答案


我有一些接收和处理大图像的PHP代码。在处理图像以使用jQuery更新DOM时,我想在某些点回应一些JavaScript。

I have some PHP code that is receiving and processing large images. I'd like to echo out some JavaScript at certain points while the image is being processed to update the DOM with jQuery.

这可能超出你必须完成的任务,但我会使用AJAX。你当然可以得到你想要的东西,但从长远来看这种方法并不好。

This may be out-of-scope for what you have to get done, but I'd use AJAX for this. You can certainly get what you want to occur, but the approach isn't good in the long term.

而不是提交整个页面并等待它回来在爬行时,使用AJAX请求上传图像并获得结果。然后,客户端上的计时器可以发出单独的 AJAX你做得多远?要求。这两个PHP实例将通过在数据库中的作业条目上设置完成标志来进行通信。

Instead of submitting the whole page and waiting for it to come back at a crawl, use an AJAX request to upload the image and get the result. Then a timer on the client can issue separate AJAX "how far done are you?" requests. The two PHP instances would communicate via setting a "done" flag on the job entry in a database, etc.

虽然它使客户端的东西更复杂一些,处理用户交互(例如允许用户取消长时间运行的作业)更容易,并使您的PHP代码更加专注。

While it makes the client-side stuff a bit more complex, it is much easier to handle user interaction (such as allowing the user to cancel a long-running job) and makes your PHP code a lot more tightly-focused.

这篇关于使用PHP输出Bufferering在处理过程中发出JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆