MegaTron
MegaTron

Reputation: 3393

Is there a way to make .next() return to the first item?

Is there a way to make .next() return to the first item? Currently, it cycles through perfectly, however, upon reaching item three, it obviously doesn't have a 'next' to go to. Is there a way to return to item one and loop through again?

<div class="item">Item1</div>
<div class="item">Item2</div>
<div class="item">Item3</div>

setInterval(function(){
 $('.item:visible').hide().next().fadeIn('fast');
},5000); //Milliseconds

Any assistance would be greatly appreciated :)

Upvotes: 1

Views: 44

Answers (2)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382264

I would do this :

(function(){
   var i = 1, items = $('.item');
   setInterval(function(){
      items.hide().eq(i).fadeIn('fast');
      i = (i+1)%items.length;
   },5000); //Milliseconds
})();

Demonstration

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try,

var cache;

setInterval(function(){
 cache = $('.item:visible').hide().next();
 (cache.length)? cache.fadeIn('fast') : $('.item:first').fadeIn('fast');
},5000); 

DEMO

Upvotes: 0

Related Questions