长Ajax调用时显示进度进度、Ajax

2023-09-10 16:14:57 作者:沵彵媽嬡過莪庅

我有一个简单的网站(http://www.kousenit.com/twitterfollowervalue)该计算基于个人的Twitter追随者的量。由于Twitter的API只返回追随者100的时间,一个完整的过程可能会涉及到大量的电话。

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.

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

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
}

这是从一个叫renderTTFV.groovy控制器调用。我使用的原型库通过Ajax调用呼叫控制器:

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

在我的网页,在标题部分(JavaScript的):

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

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

和有一个在页面的主体部分的更新,当通话结束一个div。

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

一切工作,但updateFollowers方法可能需要很长时间相当数量。有没有一些方法,我可以返回一个进度值?例如,我想更新网页上的每个迭代。我事先知道有多少次迭代都会有。我不能想出一个办法,在这个循环的中间更新页面。

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.

任何建议将是AP preciated。

Any suggestions would be appreciated.

推荐答案

有关更多或更少精确的进度报告,你有两个选择:

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

在保持服务器告诉你它的进展 保存在客户端(浏览器)向服务器关于其进度

保持服务器告诉你的进展将是实施一种容易。你可以而不是调用,在的Ajax.Updater ,创建一个 IFRAME 元素,并修改服务器,每次迭代,翻斗用一些javascript一个响应于触发表示在浏览器上的进展,并刷新该响应。这样,浏览器会执行JavaScript,并一直等待,直到响应完成,因此用户将看到进度指示器移动,直到一切都完成了。

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.

至于获得定期询问操作进度的浏览器,就可以返回一些象征性的浏览器每次迭代,浏览器会检查,看它是否是最后的结果,还是应传递给服务器所以服务器一直打到Twitter的下一个结果集,或以某种方式保持状态(如果你有会话状态的支持,有可能做到这一点)在你的服务器在每次迭代更新,可以在一个单独的请求被轮询。

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.

我希望建议将帮助。