Hi All, using the following javascript code to randomly rotate images... <!-- Begin var interval = 2.5; // delay between rotating images (in seconds) var random_display = 1; // 0 = no, 1 = yes interval *= 1000; var image_index = 0; image_list = new Array(); image_list[image_index++] = new imageItem("http://www.website.com/images/pic1.jpg"); image_list[image_index++] = new imageItem("http://www.website.com/images/pic2.jpg"); image_list[image_index++] = new imageItem("http://www.website.com/images/pic3.jpg"); var number_of_image = image_list.length; function imageItem(image_location) { this.image_item = new Image(); this.image_item.src = image_location; } function get_ImageItemLocation(imageObj) { return(imageObj.image_item.src) } function generate(x, y) { var range = y - x + 1; return Math.floor(Math.random() * range) + x; } function getNextImage() { if (random_display) { image_index = generate(0, number_of_image-1); } else { image_index = (image_index+1) % number_of_image; } var new_image = get_ImageItemLocation(image_list[image_index]); return(new_image); } function rotateImage(place) { var new_image = getNextImage(); document[place].src = new_image; var recur_call = "rotateImage('"+place+"')"; setTimeout(recur_call, interval); } // End --> Code (markup): ...however, I no longer want it random and would like it to go in order. Can this be done with this peice of code?
Well, first up I'd ease off on the use of functions for no reason other than overhead - javascript is an interpreted language after all. After cleaning it up to follow camelback naming conventions, this is what I'd use there: var imageInterval=2500; // delay between rotating images (in ms) var imageRandom=false; var imageList=new Array(); imageList[imageList.length]=new imageItem("images/pic1.jpg"); imageList[imageList.length]=new imageItem("images/pic2.jpg"); imageList[imageList.length]=new imageItem("images/pic3.jpg"); var imageIndex=imageList.length; function imageItem(pathToImage) { this.imageItem=new Image(); this.imageItem.src=pathToImage; } function rotateImage(place) { imageIndex=(imageRandom) ? Math.floor(Math.random()*imageList.length) : imageIndex=(imageIndex+1)%imageList.length; document[place].src=imageList[imageIndex].imageItem.src; setTimeout("rotateImage('"+place+"')",imageInterval); } Code (markup):
I think you will be very impressed with my script... check out my sig.. it does all the work... PM me if you are interested
Sorry mystery, but I think he'll be better off with Jason's re-vamped script. Especially given that it's been given freely and that he didn't have to solicit anybody to get it.
...my mistake and apologies. didnt even realize there was a javascript forum. If the moderators would kindly move this thread or close it altogether as this issue has been resolved.