0

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What's the difference between calling rotador(i) in the timeout closure and returning it as a new function? I am curious :)
The value of i. We are returning a function in a new scope, when i is defined with the specific value. If we just call it in a function, it will have the value of i of the outer scope, which will be always the last one - 5 in the case
3

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

Comments

1
rotationStep(1);

function rotador(id)
{
    console.log(id);
}

function rotationStep( currentId )
{
    rotador(currentId);
    var nextId = currentId<5 ? currentId+1 : 1;
    setTimeout(function(){ rotationStep(nextId) },2000); //anonymous function is a way to pass parameter in IE
}

1 Comment

Did you remove function rotador(id) { console.log(id); } ?
0

Use a callback:

setTimeout(function() {
    rotador(i)
}, 2000)

Comments

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.