for Loop not working in javascript? [closed]

Maybe this is what you want?

function setImage(i) {
    var image = "<img src=images/sl" + i + ".jpg" + "  width="500" height="300" style="margin-left:400px;" />";
    document.getElementById("slider").innerHTML = image;
}

function loopImages(i) {
    setImage(i);    //    set current Image

    if (i == 5) {    
      i = 1;    //  reset the counter
    }

    setTimeout(function () {
        loopImages(i++);
    }, "600");    //    set next image in 600 ms

}

loopImages(1);

Don’t know if you want it to stop on image 5 or to loop forever…

Browse More Popular Posts

Leave a Comment