středa 2. prosince 2015

Periodical Call of Javascript Function using SetTimeout

The following example shows how to set up a periodical function call of JavaScript function inside an html page.




<html>
<head>

<script>
var imageRefs = ["pict1.png","pict2.png","pict3.png"];
var counter = 0;

(function changeImage(){
 
 image = document.getElementById("image1");
 if(image != null) {
   image.src = imageRefs[counter];
   counter = (counter + 1) % 3;
   }

 setTimeout(changeImage,2000);

})();
</script>

</head>
<body>

<img id="image1" src="pict1.png" />

</body>

</html>
Here is the working example:

Žádné komentáře:

Okomentovat