如何在等待服务器进程时使用 jQuery 和 PHP 制作进度条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18735848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make progress bar with jQuery and PHP while waiting server process?
提问by Ifan Iqbal
It used to be easy to find solutions of my problem when Google gives many accurate results that point to Stackoverflow. But, I didn't found one for this problem. If any of you have suggestion where should I go for this problem, please give me link to the answer.
当 Google 提供许多指向 Stackoverflow 的准确结果时,过去很容易找到我的问题的解决方案。但是,我没有找到解决这个问题的方法。如果你们有任何建议我应该去哪里解决这个问题,请给我答案的链接。
The case is I want to make searching index from database. The indexing processis triggered by jQuery.getJSON
. It takes minutes to finish so I want to make progress bar to show that indexing process. I know how to make common ajax request using .getJSON
as client side and PHP as server side, but It seems difficult for me to imagine how to make such progress bar.
情况是我想从数据库中搜索索引。该索引过程被触发jQuery.getJSON
。完成需要几分钟,所以我想制作进度条来显示索引过程。我知道如何使用.getJSON
客户端和 PHP 作为服务器端发出常见的 ajax 请求,但我似乎很难想象如何制作这样的进度条。
Is there any body that can explain to me how to do that?
有没有机构可以向我解释如何做到这一点?
回答by trrrrrrm
- check Jquery Progress bar UI : http://jqueryui.com/progressbar/
- use .getJSON or an ajax call every 10 seconds to call the server and check the progress
- after you get the server progress update the UI
- 检查 Jquery 进度条 UI:http: //jqueryui.com/progressbar/
- 每 10 秒使用 .getJSON 或 ajax 调用调用服务器并检查进度
- 获得服务器进度后更新 UI
Explaining Step2
解释 Step2
step 2 is generally based on your application flow, lets say for example i'm inserting 100,000 records in MYSQL, i would create another service that will check the rows count and the result were 30,000 records for example, that means the progress now is 30%, so if i call this service every 10 seconds i would get the current progress.
第 2 步通常基于您的应用程序流程,例如我在 MYSQL 中插入 100,000 条记录,我将创建另一个服务来检查行数,结果是 30,000 条记录,例如,这意味着现在的进度是 30 %,所以如果我每 10 秒调用一次此服务,我将获得当前的进度。
回答by Marek
You can output javascript from your long running script:
您可以从长时间运行的脚本中输出 javascript:
while(doThings()) {
echo '
<script> updateProgress("' . $x .'%");</script>';
flush();
}
I personaly use bootstrap: http://getbootstrap.com/components/#progress
我个人使用引导程序:http: //getbootstrap.com/components/#progress