So let’s say I have five divs. I am trying to show the first 3 divs and then showing a new div and removing a div on a set interval.
Here’s my HTML code:
<div class="speaker">One</div>
<div class="speaker">Two</div>
<div class="speaker">Three</div>
<div class="speaker">Four</div>
<div class="speaker">Five</div>
The JS Demo is here http://jsfiddle.net/mirzar/Sny3e/8/.
Any idea on what I am doing wrong in terms of not keeping the current index? Once it has show the Fifth speaker, I am trying to iterate back to go from “Three Four Five” to “Two Three Four” and then “One Two Three” and then re-starting from the beginning
I forked your fiddle and tried to not modify as much as possible. I set a flag to check which direction we are going up or down. When we are going back down the index we check to see if were back at 0 again, if so we reset all of our index positions to their default.
When you are using the current index as the condition on each pass, what ends up happening is you get to a point where it teeters back and forth between passing and failing. Which is why it is better, to set a ‘flag’ variable to dictate which set of statements should be run.
This can be cleaned up quite a bit but I think it’s working as you were originally intending it to.
Hope it helps
http://jsfiddle.net/ZjcYd/