Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to change an image every 3 seconds?

    How do i change an image every 3 seconds between 3 images and on a loop? What is the simplest way to do it?

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    59
    Thanks
    5
    Thanked 4 Times in 4 Posts
    use javascript's setInterval. array with your images and change the value of the img tag when the interval expires?

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I tried this and it does nothing

    Code:
    var AngusYoungInt = 0;
    var AngusYoung = new Array();
    AngusYoung[1] = new Image();
    AngusYoung[1].src = "AngusYoung2.jpg";
    
    
    var int=setInterval("changeAdd()",3000)
    
    function changeAdd()
    {
    AngusYoungInt = AngusYoungInt + 1;
    document.getElementById("add1").src = "AngusYoung[AngusYoungInt]";
    }

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    135
    Thanks
    13
    Thanked 10 Times in 10 Posts
    Code:
    var imgArray = []
    
    imgArray[0] = "image1.jpg"
    imgArray[1] = "image2.jpg"
    imgArray[2] = "image3.jpg"
    imgArray[3] = "image4.jpg"
    
    var i = 0; //set a counter to keep track of where we are in the array
    
    function imgChanger(){
    	if(i == imgArray.length){ //if we are on the last item of the array
    		clearInterval(timer); //clear the interval set
    		return; //and exit the function
    	}
    	alert(imgArray[i]); //your code here
    	i++; //increment i each time the function is run
    	var timer = setInterval( "imgChanger()", 3000 );  //setInterval triggers this function, (specified in quotes) every 3 seconds.
    }
    check out http://www.w3schools.com/js/js_timing.asp for how setInterval works
    Last edited by mike182uk; 06-28-2009 at 10:19 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •