在长时间 Ajax 调用期间显示进度

发布于 2024-08-27 18:55:06 字数 1118 浏览 6 评论 0原文

我有一个简单的网站 (http://www.kousenit.com/twitterfollowervalue) 计算数量基于一个人的 Twitter 关注者。由于 Twitter API 一次仅返回 100 个关注者,因此完整的过程可能涉及大量调用。

目前,我对运行 Twitter 循环的方法进行了 Ajax 调用。该方法看起来像 (Groovy):

def updateFollowers() {
    def slurper = new XmlSlurper()
    followers = []
    def next = -1
    while (next) {
        def url = "http://api.twitter.com/1/statuses/followers.xml?id=$id&cursor=$next"
        def response = slurper.parse(url)
        response.users.user.each { u ->
           followers << new TwitterUser(... process data ...)
        }
        next = response.next_cursor.toBigInteger()
    }
    return followers
}

这是从名为 renderTTFV.groovy 的控制器调用的。我使用原型库通过 Ajax 调用来调用控制器:

在我的网页上的标头部分 (JavaScript):

function displayTTFV() {
    new Ajax.Updater('ttfv','renderTTFV.groovy', {});
}

并且页面正文中有一个 div,该 div 会在调用完成时更新。

一切正常,但 updateFollowers 方法可能需要相当长的时间。有什么方法可以返回进度值吗?例如,我想在每次迭代时更新网页。我提前知道会有多少次迭代。我只是想不出一种方法来更新该循环中间的页面。

任何建议将不胜感激。

I have a simple web site (http://www.kousenit.com/twitterfollowervalue) that computes a quantity based on a person's Twitter followers. Since the Twitter API only returns followers 100 at a time, a complete process may involve lots of calls.

At the moment, I have an Ajax call to a method that runs the Twitter loop. The method looks like (Groovy):

def updateFollowers() {
    def slurper = new XmlSlurper()
    followers = []
    def next = -1
    while (next) {
        def url = "http://api.twitter.com/1/statuses/followers.xml?id=$id&cursor=$next"
        def response = slurper.parse(url)
        response.users.user.each { u ->
           followers << new TwitterUser(... process data ...)
        }
        next = response.next_cursor.toBigInteger()
    }
    return followers
}

This is invoked from a controller called renderTTFV.groovy. I call the controller via an Ajax call using the prototype library:

On my web page, in the header section (JavaScript):

function displayTTFV() {
    new Ajax.Updater('ttfv','renderTTFV.groovy', {});
}

and there's a div in the body of the page that updates when the call is complete.

Everything is working, but the updateFollowers method can take a fair amount of time. Is there some way I could return a progress value? For example, I'd like to update the web page on every iteration. I know ahead of time how many iterations there will be. I just can't figure out a way to update the page in the middle of that loop.

Any suggestions would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2024-09-03 18:55:06

对于或多或少准确的进度报告,您有两种选择:

  • 让服务器告诉您其进度
  • 让客户端(浏览器)向服务器询问其进度

让服务器告诉您进度将很容易实现。您可以不调用 Ajax.Updater,而是创建一个 iframe 元素并修改服务器,以便对于每次迭代,使用一些 javascript 转储响应以触发显示进度在浏览器上,并刷新该响应。这样,浏览器将执行 JavaScript 并继续等待,直到响应完成,因此用户将看到进度指示器向上移动,直到一切完成。

服务器还可以使用其他方法来告诉您操作的进度。您可以通过 Bing/Google 了解 Comet 服务器。

至于让浏览器定期询问操作的进度,您可以在每次迭代时向浏览器返回一些令牌,浏览器将检查它是否是最终结果,或者是否应该将其传递给服务器,以便服务器不断点击 twitter 获取下一个结果集,或者以某种方式在服务器中保留一个状态(如果您有会话状态支持,这可能会做到这一点),该状态在每次迭代中更新,并且可以根据单独的请求进行轮询。

我希望建议有所帮助。

For more-or-less-accurate progress reporting you have two alternatives:

  • Keep the server telling you its progress
  • Keep the client (browser) asking the server about its progress

Keeping the server telling you the progress would be kind of easy to implement. You can, instead of calling the Ajax.Updater, create an iframe element and modify the server to, for each iteration, dump a response with some javascript to trigger showing the progress on the browser, and to flush that response. Like that, the browser will execute the javascript and keep waiting until the response finishes, so the user will see the progress indicator moving up until everything's completed.

Other approaches are available for the server to tell you about the operation's progress. You can Bing/Google about Comet servers.

As for getting the browser to periodically ask about the operation's progress, you can either return some token to the browser for each iteration that the browser would check to see if it's the final result or if it should pass it on to the server so the server keeps hitting twitter for the next result set, or somehow keep a state (if you have session state support, that might do it) in your server that is updated in each iteration and that can be polled on a separate request.

I hope the suggestions help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文