Titanium Community Questions & Answer Archive

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

Android setTimeout() workarounds?

Titanium 1.6.1, Android APIs 2.3

The problems with the various setTimeout() methods on Android are well-documented both here and on Lighthouse, and given their persistence they seem to be a bit of a tough obstacle for Appcelerator to overcome.

I have two instances at the moment where (different) setTimeouts are falling down.

  1. A splash screen that I want to disappear after n milliseconds. However, sometimes the setTimeout() function is never called, and the splash screen stays showing. There seems to be no predictability to it working or not.

  2. An HTTPClient request that on occasion never returns (neither onload nor onerror ever gets called), so an ActivityIndicator that gets show before send() is never dismissed. Unfortunately on Android the ActivityIndicator is modal, so that presents a problem (not even the Back button works to dismiss it). Setting a timeout on the request itself doesn't work (it seems Android doesn't honor the timeout, or something), and trying to create a supervisor setTimeout() doesn't work reliably either, presumably for the same reason as #1.

How are other people working around setTimeout() on Android?

Is there a random number generator in the Titanium Java code to determine if setTimeout() will work this time or not?

— asked April 5th 2011 by K T
  • android
  • httpclient
  • settimeout
  • xhr
0 Comments

1 Answer

  • I have also had much difficulty with both setTimeout and HttpClient – exactly the same behavior as you describe: no errors, callbacks just never get called. I'm still working on pinning down what causes the issue with HttpClient, but I just solved an issue with setTimeout:

    I was doing something like:

    setTimeout(function() {
     // do cool stuff
    }, 100);
    

    That just didn't do anything. I extracted the code to its own function and it worked fine:

    var doit = function() { /* cool stuff */ };
    setTimeout(doit, 100);
    

    That worked. It doesn't seem to be a cure-all for setTimeout, but in this case it worked.

    — answered April 7th 2011 by Peter Coley
    permalink
    4 Comments
    • Thanks. Doesn't seem to solve it for me, but I'm glad it did for you (at least in this case).

      Of course it may also be that I'm not doing nearly enough / cool stuff / in my function…

      — commented April 7th 2011 by K T
    • Didn't solve for me either. Anyone have any info?

      — commented July 13th 2011 by Paul Shapiro
    • That worked for me. Thanks.

      — commented November 9th 2011 by Evan McDaniel
    • worked for me as well, thanks!

      — commented December 8th 2011 by Daniel Urstöger
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.