Show progress during a long Ajax call
- by kousen
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.