0

I'm trying to add a sleep/delay function to make an animation that the div slides out. At the moment it will instantly turn -60em without any delay at all which prevent the div from slide out.

Dunno why setTimeout ignores the time delay.

for(activeSlide; activeSlide < i; activeSlide++){
    animateLeft(activeSlide);

    function animateLeft(activeSlide){
        div[activeSlide].style.left = step+'em';
        step -= 6;

        if(step < -60) {
            return; 
        }
        setTimeout(animateLeft(activeSlide), 5000);
    }
}
Nicco
  • 286
  • 1
  • 2
  • 15

1 Answers1

2

You're calling animateLeft(activeSlide) immediately and setting a timeout to call its return value in 5 seconds.

You want this instead:

setTimeout(function() { animateLeft(activeSlide); }, 5000);

Or else this:

setTimeout(animateLeft, 5000, activeSlide);
Mark Reed
  • 91,912
  • 16
  • 138
  • 175