Monday, October 15, 2012

HTML / Javascript for switching images based on a time interval

Here's a code snippet for flipping an image every second. In this example there's only 2 images. 

<!DOCTYPE html>
<html>
<body>
<img id="img" src="image2.jpg">

<script>
var images = new Array()
images[0] = "image1.jpg";
images[1] = "image2.jpg";
setInterval(changeImage, 1000);
var x=0;

function changeImage()
{
document.getElementById("img").src=images[x%2]
x++;
}



</script>

<button onclick="int=window.clearInterval(int)">Stop</button>

</body>
</html>


Source: http://stackoverflow.com/questions/3264739/image-change-every-30-seconds-loop

No comments:

Post a Comment

There was an error in this gadget