Switch image only once at a timed interval?

Discussion in 'JavaScript' started by Gatorade, Jul 8, 2008.

  1. #1
    I have two images. I need to show the first for 5 seconds and then switch to the second image and stay. I don't need the images to keep rotating.

    Could someone please show me the code for this? Positive rep for anyone that can help. :)
     
    Gatorade, Jul 8, 2008 IP
  2. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Following is the script for you. Checked it.

    <html>
    <head>
    
    </head>
    
    <body onLoad="startTime()">
    <img name="imgstar" src="images/1.jpg" border="0">
    <script type="text/javascript">
    var icount =1;
    function startTime()
    {
    
    
    t=setTimeout('startTime()',5000);
    icount = icount + 1;
    if (icount == 3)
    	exit();
    	
    document.imgstar.src="images/"+icount+".jpg"; 
    
    }
    
    
    </script>
    </body>
    
    </html>
    
    Code (markup):
    You can increase the time in milisecond using
    setTimeout('startTime()',5000);
    Code (markup):
    function.
     
    VishalVasani, Jul 8, 2008 IP
    Gatorade likes this.
  3. Gatorade

    Gatorade Peon

    Messages:
    2,130
    Likes Received:
    222
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Where do I enter the second image path?
     
    Gatorade, Jul 8, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Its better to set the first parameter of setTimeout to a function reference, not a string.
     
    MMJ, Jul 8, 2008 IP
  5. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello
    You only required to put two images in image folder named it 1.jpg & 2.jpg.
    Then this script will work for you.
     
    VishalVasani, Jul 8, 2008 IP