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.
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.
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?
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.