image rotate script

Discussion in 'HTML & Website Design' started by John84, Nov 5, 2007.

  1. #1
    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?
     
    John84, Nov 5, 2007 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    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):
     
    deathshadow, Nov 5, 2007 IP
  3. mystery

    mystery Banned

    Messages:
    744
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 ;)
     
    mystery, Nov 11, 2007 IP
  4. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Dan Schulz, Nov 11, 2007 IP
  5. soulscratch

    soulscratch Well-Known Member

    Messages:
    964
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    155
    #5
    Why not post in Javascript if you have a Javascript question?
     
    soulscratch, Nov 11, 2007 IP
  6. Dwings

    Dwings Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    its javascriopt :confused:
     
    Dwings, Nov 11, 2007 IP
  7. John84

    John84 Active Member

    Messages:
    1,288
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #7
    ...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.
     
    John84, Nov 12, 2007 IP