$(document).ready(function() { $('#background-cover').css({'visibility' : 'visible'}); var currentImg = 0; var nextImg = 0; if(photos.length>1){ imageSwitch(); } function imageSwitch() { currentImg = nextImg; nextImg++ if(nextImg==photos.length){ nextImg=0; } $('#homebackground').css({'background-image' : 'url('+photos[nextImg]+')'}); $('#background-cover').css({'background-image' : 'url('+photos[currentImg]+')'}).show().fadeOut(1000); preloadimg = new Image(); preloadimg.src = photos[nextImg+1]; setTimeout(function() {imageSwitch();}, interval); } });