如何在 JavaScript 中创建服务器端进度指示器?
我想在我的网站中创建一个部分,用户可以在其中使用一些简单的更新
按钮。
每个更新按钮都会发送到服务器,并在后台进行长时间的处理。
当服务器处理数据时,我希望用户有某种进度指示器,例如进度条或文本百分比。
我使用 jQuery 作为我的 JavaScript 库,使用 CodeIgniter (PHP) 作为服务器端框架,如果它很重要的话......
我正在考虑使用 PHP 的 flush()
函数来报告进度状态到 jQuery,但我不确定 jQuery 的 Ajax 函数是否在完成之前读取输出......
所以任何建议/解释都会有用且有帮助!
I want to create a section in my site, where a user has a few simple update
buttons.
Each of these update
buttons will be going to the server, and will do a long crunching behind the scene.
While the server crunches data, I want the user to have a some kind of progress indicator, like progress bar or textual percentage.
I'm using jQuery as my JavaScript library, and CodeIgniter (PHP) as the server-side framework, if it's important...
What I was thinking about is using PHP's flush()
function to report progress status to jQuery, but I'm not sure that jQuery's Ajax functions are reading the output before it's complete...
So any advice/explanation would be useful and helpful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将为您提供一个使用 WebSync On-Demand 的示例,但无论如何,相同的方法都可以工作您选择的服务器。
这就是你要做的。首先,以某种方式启动长时间运行的操作;您的用户单击按钮来启动此过程(我将假设 Ajax 调用,但无论有效),然后您返回给他们某种标识符,我们将其称为“myId”,给它一个值“ 1'.您是否通过调用某种过程等来做到这一点取决于您。
然后,在该调用的回调中,您将编写如下内容:
这样做的作用是订阅您的客户端以接收有关任务更新的通知,因此剩下的就是推送更新,您将执行此操作在实际运行任务的任何进程中。看起来像(在 PHP 中,使用 SDK):
就是这样;当更新发生时,它们会实时推送给您的客户。
I'm going to give you an example using WebSync On-Demand, but the same approach would work regardless of your choice of server.
Here's what you do. First, kick off the long-running operation somehow; your user clicks the button to start this process (I'm going to assume an Ajax call, but whatever works), and you return to them some sort of identifier, we'll call that 'myId', give it a value of '1'. Whether you do that by invoking a process of some sort, etc, is up to you.
Then, in your callback from that invocation, you would write something like so:
What that'll do is subscribe your client to receive notification about updates to the task, so all that's left is to push out the updates, which you'd do in whatever process is actually running the task. That would look like (in PHP, using the SDK):
That's it; as updates occur, they'll push out to your client in real-time.
要做到这一点非常困难。我们为我们的系统选择的是一个“伪造的”进度条——它只是一遍又一遍地动画(因为它是一个动画 gif,你可能会想到!)。
另一种方法是提交到一个脚本,并在后台进行该处理(并将进度输出到文件),同时向另一个脚本发出 Ajax 请求,该脚本的唯一责任是读取该进度文件并返回您所完成的进程的进度。是。这会起作用 - 感觉有点笨拙,但它至少可以解决你眼前的问题。
我对Comet之类的了解很少,所以这纯粹是基于我目前的理解。
It's pretty hard to get this right. What we've settled on for our system is a "faked" progress bar - it just animates over and over (which since it is an animated gif, you might expect!).
An alternative would be to submit to one script, and have that processing in the background (and outputting progress to a file) while making an Ajax request to another script whose only responsibility is to read that progress file and return how far through the process you are. This would work - it feels a little bit kludgy, but it would at least solve your immediate problem.
I know very little about Comet or the likes, so this is purely based on my current understanding.
晚了三年,但这是我想出的解决方案。额外奖励:它适用于 IE7+
使用:
事件表:
HTML:
PHP:
如果您想了解
PDOStatementWrapper
,它的来源是 < a href="https://github.com/knyri/simple-php-library/blob/master/lib/lib/db/pdo_database.functions.php" rel="nofollow">此处。抱歉,它不包含与 CodeIgniter 集成的任何内容。3 years late, but here's a solution I came up with. Bonus: It works in IE7+
Uses:
The event table:
The HTML:
The PHP:
In case you were wondering about
PDOStatementWrapper
the source for it is here. Sorry it doesn't include anything integrated with CodeIgniter.