views:

146

answers:

2

I want mootools to change the background color constantly. I have no idea how to do this but I have come up with the following code so far as a test but it doesn't work at all. How can I chain colors and then call it again and again so I have this "rainbow" background?

function rainbow() {
    $(document.body).highlight("#fff");
    rainbow();
}
+2  A: 

You could use setInterval(); and .getRandom();

setInterval(function(){
  var newColor = ["#FFF","#CCC","#999"].getRandom();
  $(document.body).highlight(newColor);
}, 1000); // run every 1 second
Jonathan Sampson
Is there a way I could make it loop through a set order of colors?
ba
ba, I've provided an example of how to pull a random color using `.getRandom();`. You could modify it to find the index of whichever color presently is being used, and use the next (or the first, if you're at the end of the array).
Jonathan Sampson
Thanks, you're awesome.
ba
You're welcome, ba. Welcome to StackOverflow!
Jonathan Sampson
+1  A: 

You need to use chaining here. Example:

var color = "#fff";
function rainbow() {
    $(document.body).highlight(color).get('tween').chain(rainbow);
    color = (color == "#fff") ? "#000" : "#fff";
};
rainbow();

That's a simple example. But you can see how the chaining works; you call a function when the effect completes. What color you choose every iteration is up to you; here I just cycle from black to white and back.

Anutron