setInterval or Another Alternative?

You must Login before you can answer or comment on any questions.

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.

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 3 years ago by Jacob Williams
answer 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 2 years ago 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 2 years ago by Romain Salles

  • Thanks for that! I don't suppose you know what setInterval doesn't fire on android ?

    — commented 2 years ago by Chris Brooks

Your Answer

Think you can help? Login to answer this question!