I want an image to change every second. I’m having trouble with setInterval. Could someone post a quick snippet on how to do this
This is what I came up with.
var images = 'images/image_*.png';
for(var i = 1; i <= 5; i++){
function changeImg(){
var path = images.replace('*', i);
$('img').attr('src', path);
}
setInterval('changeImg()', 1000);
}
In your code you are calling the setInterval function 5 times which really is not necessary. Also as the loop will execute once, the value of
iwill always be 5 so it won’t work as you expect. You may try this instead: