Q:

Javascript function call into loop each time

I have function called rotator(id): this function animate div and I can called this function with different id for animate different elements
Actually I use 5 differents id , 1,2,3,4,5
And for call I need put :
rotador(1);rotador(2);rotador(3);rotador(4);rotador(5);

The problem it´s that I want to rotate in automatic mode. For this I think to use this 
for (i=0;i<=5;i++) { 
   setTimeout(rotador(i),2000);
}

But it doesn't work because it animates all in the same time, no let firt execute the first and continue before of first go second , etc , etc and when go the end or number 5 start other time in one 
My problem it´s this if you can help me THANKS !!! :) Regards

A:

You are actually calling the rodator(i) function, and schedule for execution after 2 seconds the result of the rodator. In other words, your code is now equalent to:
for (i=0;i<=5;i++) { 
   var result = rotador(i);
   setTimeout(result,2000);
}

You can accomplish this either by creating a function for the callback:
for (i=0;i<=5;i++) { 
   setTimeout((function(i){
      return function(){
        rotador(i);
      }
    })(i),2000 * i);
}

or you can call the next rodator in the rotador function itself:
var rotador = function(i){
    // your code
    if (i < 5) {
       setTimeout(function(){rotaror(i + 1);}, 2000);
    }
}

Note: the closure in the second example is needed to call the function with the correct value of i. We are creating an anonymous function, and create i as a local scope variable, which value won't be mutated by the outerscope changes. (we can rename i to n in the local scope, if this would be more readable). Otherwise the value of i will be 5 each time rotador is called, as the value of i would be modified before the actual function call.

A:

since setTimeout() does not wait for the function to be executed before continuing, you have to set the delay to a different value for different items, something like 2000 * (i + 1) instead of just 2000
EDIT: yes, and you need the callback as Darhazer suggests

