4

Fiddle here: http://jsfiddle.net/F6nJu/

I have a colored box:

<div id="colorblock"></div>
#colorblock { background:#3ff; width: 100%; height: 300px; }

I have an array of colors created in javascript:

var arr = [ "#f00", "#ff0", "#f0f", "#f66"];

I iterate through those colors with a jQuery each() function:

$.each(arr, function(key, value) {
  $('#colorblock').delay('1200').animate({backgroundColor:value}, 600);
});

When the array iterates to the end, how can I start the array iteration over (so the animation goes on forever)?

1
  • Why iterate when you can just use array indexes? i=0; arr[i++ % arr.length] Commented Sep 7, 2016 at 16:06

4 Answers 4

11
var arr = ["#f00", "#ff0", "#f0f", "#f66"];

// run through the array forever
(function recurse(counter) {
    // get the colour
    var color = arr[counter];
    // animate it
    $('#colorblock').delay('1200').animate({
        backgroundColor: color
    }, 600);
    // delete the value to save memory
    delete arr[counter];
    // add the value at the end of the array
    arr.push(color);
    // run it again for the next number
    setTimeout(function() {
        recurse(counter + 1);
    }, 200);
// start it for the first number.
})(0);

Infinite recursion. Delete the current entry, then add the current value to the end.

Live Example

For those who are interested in what the array looks like:

["#foo", "#ff0", "#f0f", "#f66"] (counter = 0)
[undefined, "#ff0", "#f0f", "#f66", "#foo"] (counter = 1)
[undefined, undefined, "#f0f", "#f66", "#foo", "#ff0"] (counter = 2)
[undefined, undefined, undefined, "#f66", "#foo", "#ff0", "#f0f"] (counter = 3)
[undefined, undefined, undefined, undefined, "#foo", "#ff0", "#f0f", "#f66"] (counter = 4)
etc.
Sign up to request clarification or add additional context in comments.

5 Comments

i assume you are using my set_color function?
please... (we are here to learn so... ;) The last line of your code: })(0); i played with numbers: 0, 1, 2, 3. Interesting things happend. What is this exactly?
@roXon added more details / comments.
@Raynos... You are so kind! You just earned a +10! (I just can't find the button ;) well... +1
Perfect solution... but how to toggle it? I'm trying using jQuery .toggle() with stop() on recurse()...
5

Try this:

var arr = ["#f00", "#ff0", "#f0f", "#f66"];
$.each(arr, function(key, value) {
    set_color(value);
});

function set_color(color) {
    $('#colorblock').delay('1200').animate({
        backgroundColor: color
    }, 600);
    setTimeout(function() {
        set_color(color);
    }, 2000); //restart in 2 seconds
}

Fiddle: http://jsfiddle.net/maniator/F6nJu/1/

6 Comments

this works, thank you! the setTimeout() function does not seem to work, but no matter, as I have set a delay before the animation.
@Neal... why is there a setTimeout if delay is set? ... if I remove the timeout the animation stops. What is actually doing setTimeout? thanks in advance!
@superUntitled, its 2 secs for that color, not any color
@roXon setTimeout is recursing the function
$.each for an array that isn't jQuery elements? Yuck. Use a for or while loop.
|
2
Array.prototype.recurse = function(callback, delay) {
   delay = delay || 200; 
   var self = this, idx = 0;

   setInterval(function() {
      callback(self[idx], idx);
      idx = (idx+1 < self.length) ? idx+1 : 0;
   }, delay);
}

Try it on StackOverFlow:

["#f00", "#ff0", "#f0f", "#f66"].recurse(function(item, index) { 
    $('body').css('background-color', item);
}, 300);

Comments

0
var arr = ["#f00", "#ff0", "#f0f", "#f66"];

(function recurse(counter) {
    var arrLength = arr.length;
    var index = counter%arrLength;

    var color = arr[index];

    $('#colorblock').delay('1200').animate({
       backgroundColor: color
     }, 600);

    setTimeout(function() {
       recurse(counter + 1);
    }, 200);

})(0);

1 Comment

Being posted 5 years after question was asked and accepted I recommend you add some details on why this solution is better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.