jQuery(document).ready(function(){ var speed = 700; jQuery('div.main-history-image div.main-history-container img').first().addClass('image-current'); function changeImage (target) { target.fadeIn(0); jQuery('.image-current').fadeOut(speed, 'linear', function() { jQuery('.image-current').removeClass('image-current'); target.addClass('image-current'); }); } var timer = setInterval(function() { if( jQuery('.image-current').next()[0] != null ) { changeImage( jQuery('.image-current').next() ); } else { changeImage( jQuery('div.main-history-image div.main-history-container img').first() ); } },8000); });