Currently the script rotates images every page refresh, however I want it to only rotate the images every 60 seconds. Here is the script, thanks for the help: var images = new Array(); images[0] = "images/1.jpg"; images[1] = "images/2.jpg"; images[2] = "images/3.jpg"; images[3] = "images/4.jpg"; function ShowImg() { var number = images.length; var increment = Math.floor(Math.random() * number); var strTemp ='<img src="' + images[increment] + '">'; document.write(strTemp); } var rotate = 5000; var count = 0; function RotateImg(myImage){ myImage.src=images[count]; count++; if(count==images.length){count = 0;} setTimeout("RotateImg(myImage)",rotate); } Code (markup): I put this between the head tags: <script type="text/javascript" src="rotate.js"></script> Code (markup): And this where I want the image/s to go: <script language="javascript">ShowImg();</script> Code (markup):
That's your problem. I think you should have an image tag with a name like And then use this to change the src
Thanks for your help but I don't have a clue what your talking about I don't know much about code lol, but i am trying to learn!
What they mean is you should have an element already on the page. In this example I have an <img> tag with and id="RotateImg". So rather than doing a document.write just reference the image and change its source. <head> <title>Untitled</title> <script language="javascript"> var images = new Array(); images[0] = "images/1.jpg"; images[1] = "images/2.jpg"; images[2] = "images/3.jpg"; images[3] = "images/4.jpg"; function ShowImg() { var number = images.length; var increment = Math.floor(Math.random() * number); document.getElementById("RotateImg").src = images[increment]; } var rotate = 5000; var count = 0; function RotateImg(myImage){ myImage.src=images[count]; count++; if(count==images.length){count = 0;} setTimeout("RotateImg(myImage)",rotate); } </script> </head> <body> <img src="1.jpg" id="RotateImg"> </body> </html>
Thanks for helping, does that make it so the image will only rotate at the time I set, even if the page is refreshed?
once the page is refreshed the script would reinitialize. Javascript doesn't maintain state when the page is refreshed.