使用带加载消息的ob_flush的PHP时间延迟 [英] PHP time delay using ob_flush with loading message

查看:68
本文介绍了使用带加载消息的ob_flush的PHP时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个访问某些MySQL数据库的PHP脚本,我需要等待几秒钟,直到上一个脚本将所有信息输入数据库为止。等待时间不应该太长,但是以防万一服务器在放假的日子里很慢,我正在使用sleep(10)等待10秒钟,然后再执行脚本。我想在等待10秒钟时显示一条请稍候消息,但是不幸的是,仅在10秒钟完成后才显示该消息。我就是这样的:

I'm running a PHP script that accesses some MySQL databases, and I need to wait a few seconds until the previous script has entered all the information into the database. The wait time shouldn't be long, but just in case servers are slow on an off day, I'm using sleep(10) to wait 10 seconds before executing the script. I wanted to display a "please wait" message, while it is waiting that 10 seconds, but unfortunately that message gets displayed only after the 10 seconds has already been completed. This is the way I'm going at it:

ob_start();
echo "Please wait while your invoice is being created... </br>";
ob_flush();
sleep(10);
ob_end_clean();
echo "Success...";

基于一些在线研究,我觉得ob_flush()会在10秒钟了,然后ob_end_clean()会擦除先前打印的文本,但是我得到的是10秒钟后两个文本都将显示,并且第一个文本没有被擦除。你们知道我在这里做错什么吗?

Based on some research online, I was under the impression that ob_flush() would output the text before the 10 seconds was up, and then ob_end_clean() would erase the previously printed text, but what I'm getting instead is that both texts are being displayed after the 10 seconds is up and the first text is not being erased. Do you guys know what I'm doing wrong here?

推荐答案

您无法使用PHP编辑已经输出的内容,这会需要重新加载页面。 ob_flush仅输出缓冲区并将其清空。 ob_end_clean()停止输出缓冲,使您可以再次正常输出。

You cannot edit anything you have outputted already with PHP, that would require a page reload. ob_flush just outputs the buffer and empties it. ob_end_clean() stops the output buffering, allowing you to output normally again.

要隐藏显示的文本,您将输出一个javascript以隐藏先前呈现的文本(最好包裹在div左右以便于选择)。

To hide shown text you would output a javascript to hide the previously rendered text (preferably wrapped in a div or so for ease of selecting).

这篇关于使用带加载消息的ob_flush的PHP时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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