Why is Javascript function not being called? -
The first question on this site is that I hope I will correct it! I have a JavaScript function that I want to display an image (image1.jpg) when the page loads, and then every 2 seconds the image changes through the loop. Although only the first image is showing, it seems that the JS function is not being asked. Can anyone tell me that I am doing something wrong here because it looks good for me, so it does not understand why it will not work. Thanks
& lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" & gt; Function Display Images () {var images = ['image1.jpg', 'image2.jpg', 'image3.jpg']; Var i = 1; If (i> images.lamp -1) {this.src = images [0]; I = 1; } And {it.source = picture [i]; I ++; } SetTimeout ("displayImages ()", 2000); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body onload = "displayImages ();" & Gt; & Lt; Img id = "myButton" src = "image1.jpg" /> & Lt; / Body & gt; & Lt; / Html & gt; You must move the line
I = 1;
this does not refer to the image object, so I fixed it and made the argument a little simpler: & lt; Script type = "text / javascript" & gt; Function display image (i) {var images = ['image1.jpg', 'image2.jpg', 'image3.jpg']; Var img = document.getElementById ('myButton'); Img.src = Picture [i]; I = (i + 1)% images.length; SetTimeout (function () {displayImages (i);}, 2000); } & Lt; / Script & gt; & Lt; Body onload = "displayImages (0);" & Gt;
Comments
Post a Comment