Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Best way to confirm xhr download completion

I'm downloading a file via xhr. However sometimes my process will hang even though the download has actually completed. What's the best way to confirm this?

I've tried e.progress == 1 under the ondatastream function. I've also tried readyState == 4 under the onreadystatechange function. Neither is producing consistent results.

— asked May 8th 2010 by Jordan de Laune
  • iphone
  • xhr
0 Comments

5 Answers

  • I've hit this too, and I was actually going to build a reproducible test case for a bug report today.

    I think it's a bug in the iPhone implementation, since a quick test doesn't seem to trigger it on Android. If I put a button on my app that triggers an xhr request and an alert in the onload function, then loop through download/ok/download/ok a few times, the onload fails to trigger. If I start another download, I find two alerts pop up at once, so I guess it's a timing problem or race condition. I wonder if we can work around it with a timer function?

    — answered May 8th 2010 by Damien Elmes
    permalink
    0 Comments
  • Good news - I was able to work around it with setInterval(). There's no need to do anything in the interval routine - the mere act of triggering the timer seems to cause the onload signal to be reliably delivered (though at a delay relative to the timer interval)

    — answered May 9th 2010 by Damien Elmes
    permalink
    0 Comments
  • Where do you trigger it? In which function. Can you post an example. Cheers

    — answered May 9th 2010 by Jordan de Laune
    permalink
    0 Comments
  • All you need to do is call setInterval() before you run your XHR code. It doesn't matter what the called function does.

    — answered May 9th 2010 by Damien Elmes
    permalink
    1 Comment
    • do you run this code Before… or after the XHR code.. you say before. Are you sure its not after?

      — commented October 7th 2010 by vincent youmans
  • The setInterval appears to stop my xhr request from sending at all. I think there's a pattern with CAMERA + XHR. But it's pretty hard to pinpoint, most of the time if I use the Gallery images to send over XHR, everything is fine. Use the camera + xhr and the first time is fine, the second time the xhr doesn't respond but the request was successful, and the 3rd time - the whole damn screen locks up.

    — answered May 12th 2010 by Brandon Corbin
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.