Reputation: 3393
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
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
})();
Upvotes: 1
Reputation: 67207
Try,
var cache;
setInterval(function(){
cache = $('.item:visible').hide().next();
(cache.length)? cache.fadeIn('fast') : $('.item:first').fadeIn('fast');
},5000);
Upvotes: 0