var imageIndex = 0; var images = ["b","c"]; function nextImage() { imageIndex = (imageIndex+1)%images.length; $("#pearlPicture").get()[0].src = "image.php?id=2697&extraCode="+images[imageIndex]; $("#pearlPicture").load(function () { $(this).unbind("load"); setTimeout("nextImage()",4000); }); } $(document).ready(function () { $("#pearlPicture").load(function () { $(this).unbind("load"); setTimeout("nextImage()",4000); }); });