• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to check if all ajax objects are finished in a page?

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to do something after knowing all ajax objects are finished in a page. But it seems it's not easy to tell this. I tried this code:



But isCompleted() is consistly returning false, which means not finished yet.

Can someone find problem in the code or give me a solution to tell if all ajax objects are finished in a page?
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is surely elegant and I can't really find any specific reason why it shouldn't work. For starters, you might want to alert the readyState's of every ajaxObj you retrieve from the container[]. That should give a good indication as to where the problem may lie. Also, you might want to verify if you're calling the isCompleted() method from the correct place. Maybe at the time this method is invoked, some or the other AJAX call is yet to complete its run.

Lastly, in your cancel() method, do you really need to override the onreadystatechange attribute of the ajaxReqObj and invoke abort(), since you're only setting it to null at the end?
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I just did a debug. I found the for loop is cause of the problem. Using for(var item in this.transports) makes item having an extra element 'include' (I dont't know why), which makes it return false. If I just use for(var i=0; i<this.transports.length; i++), the function works finally.

I use setInterval() to consistly call isComplete(), then the page can eventually know if all ajax object have finished it. But this leads me another doubt: what if some ajax object has waited too long and no server response, how do I write code to handle this ajax call timeout situation?
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Lastly, in your cancel() method, do you really need to override the onreadystatechange attribute of the ajaxReqObj and invoke abort(), since you're only setting it to null at the end?



I intend to cancel and stop any ajax calls when calling cancel(). I thought doing those can gracefully release the resource. Do you mean setting ajax object to null is enough to cover other 2 steps?
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way that I can think of is that instead of storing the simple ajaxObj, you can create an ajaxReq object which will look something like this:


Thereof, whenever you're looping through the container[], you can retrieve every ajaxReq object and verify whether the (currTime - ajaxReq.initTime) hasn't exceeded some pre-defined timeout value.
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anirvan. I try that out.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic