Titanium Community Questions & Answer Archive

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

setInterval or Another Alternative?

After setInterval crashed my app a few times, I went to the forums and noticed that there is currently a bug with this function. Is there any ETA on a fix, or some other alternative I can use. Basically, I am just trying to swap the BackgroundImage of a View every second or so.

— asked March 9th 2010 by Jacob Williams
  • iphone
  • mobile
  • setinterval
  • timer
0 Comments

1 Answer

  • setInterval does work, I guess I was just calling it incorrectly. For any who are interested, here is an example of how it works:

    var green = false;
    
    function changeLEDColor () {
        if (green) {
            ledView.backgroundImage = 'images/greenLED.png';
            green = false;
        }
        else {
            ledView.backgroundImage = 'images/redLED.png';
            green = true;
        }
    }
    
    setInterval(changeLEDColor,1000);
    
    — answered March 11th 2010 by Jacob Williams
    permalink
    3 Comments
    • Thanks in my app I was declaring setInterval like this which was causing the app to crash:

      setInterval(changeLEDColor(),1000);

      I am surprised removing the brackets stops the app crashing ?!

      — commented February 7th 2011 by Chris Brooks
    • It's normal.
      When you pass changeLEDColor(), you give to setInterval the result of your function (that's to say: nothing here).
      And then, setInterval will use this result as a function –> crash.

      But, if you pass changeLEDColor, you pass a reference to your function. And thus, setInterval can use it as a function.

      That's it.

      — commented February 11th 2011 by Romain Salles
    • Thanks for that!
      I don't suppose you know what setInterval doesn't fire on android ?

      — commented February 11th 2011 by Chris Brooks
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.