var imagesSlideshow = [ "assets/img/slide1.jpg", "assets/img/slide2.jpg" ]; var currentIndex = 0; //so it starts with 0, first image $(function(){ var timerSlideshow = setInterval(newPhoto, 5000); }); function newPhoto(){ $("#divSlideshow").fadeOut(100, function(){ $("#divSlideshow").css("background-image", "url("+imagesSlideshow[currentIndex]+")"); $("#divSlideshow").fadeIn(); currentIndex++; if (currentIndex == imagesSlideshow.length){ currentIndex = 0; } }); }