简体   繁体   中英

Trouble changing images with a timer with javascript

I am writing a website and I want to animate through various images on a timer. I have an array listing each of the images (as a number which is used to build the filename ). The animation should go through those numbers and in turn change the image. This code is not working and any help would be appreciated.

 <img id="myImg" src=" http://old.ycombinator.com/images/yc500.gif" width ='100' height='100'/> <script> var myImages = [1, 2, 3, 5] var img_index = 0; var timer; var imgId = "myImg"; // Bind the image onload event like this: document.getElementById(imgId).onload = animate(); // Start animation function animate() { me = document.getElementById(imgId); me.src = "coolimg." + myImages[img_index] + ".jpg" img_index++; if (img_index == myImages.length){ img_index = 0; } timer = setTimeout(animate, 1000); } </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM