html - JavaScript won't change image - Slider -
i don't understand why slider won't work.
on html page, have first time, slider, , link javascript. in javascript, have array includes 2 images. unfortunately, slider doesn't work. image won't change. stays on first one.
what might wrong?
var myimage=document.getelementbyid("slide"); var imagearray=["image1.jpg", "image2.jpg"]; var imageindex=0; function changeimage () { slider.setattribute("src", imagearray [imageindex]); imageindex++; if (imageindex>=imagearray.length) { imageindex=0; } } var intervalhandle = setinterval(changeimage,2000);
<!doctype html> <html lang="cs"> <head></head> <body> <img src="image1.jpg" id="slide"/> <script src="javascript.js"></script> </body> </html>
try following code:
var myimage=document.getelementbyid("slide"); var imagearray=["image1.jpg", "image2.jpg"]; var imageindex=0; function changeimage () { myimage.setattribute("src", imagearray [imageindex]); imageindex++; if (imageindex>=imagearray.length) { imageindex=0; } } setinterval(changeimage,2000);
Comments
Post a Comment