Edit in JSFiddle

function changeImage() {
    $('.gallery').attr('src', images[imageNo]);
    
    imageNo = (imageNo + 1) % images.length;
    if (imageNo == images.length) {
        imageNo = 0;
    }

    setTimeout(changeImage, 2000);
}

var images = [
    "http://default.secure.media.ipcdigital.co.uk/11133/0000080b0/59ea/Panasonic-Lumix-GX7-sample-image-6-small.jpg", 
    "http://www.linkorangutan.com/wp-content/themes/thesis_16/custom-sample/rotator/sample-4.jpg", 
    "http://coarsegoldhistoricalsociety.com/wp-content/themes/thesis/rotator/sample-1.jpg"
];
var imageNo = 0;

changeImage();
<img src="#" class="gallery" />