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 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    Why does this basic slide show script not work?

    Why does this basic slide show script not work?
    Code:
    <script type="text/javascript">
    var images=new Array(); // slideshow
    images[0]="27.jpeg";      
    images[1]="Capture.png";
    
    function slide(){
    
    
    for (i=0; i<images.Length; i++)
      {
    document.getElementById("slide").src = images[i];
      }
    
    
    
    }
    slide();
    </script>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Because there is no delay between the images loading into the img tag. You need a setTimeout or setInterval to call the function, can the for loop and have the function self increment when it's called.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...because it isn't a slide show?

    The net effect of that function is do nothing VISIBLE except simply
    Code:
    document.getElementById("slide").src = "Capture.png";
    And even that it will only do if the code appears *AFTER* the <img id="slide"/> in your web page.

    You have no delay whatsoever in that code, so it simply runs through the entire image array (however big it is), assigning a new src value to the <img id="slide"/> as fast as it can. Which means that, in the space of no more than a microsecond or two, it has change the src as many times as there are array elements and then quit, leaving, of course, only the very last change (the last element in the array) in place.

    So it doesn't even resemble a "slide show". If you don't understand the concepts, maybe you should go find a good one and just use it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    I tried this but nothing happens
    Code:
    <script type="text/javascript">
    var images=new Array(); // slideshow
    images[0]="27.jpeg";      
    images[1]="Capture.png";
    
    function slide(){
    
    
    for (i=0; i<images.Length; i++)
      {
    document.getElementById("slide").src = images[i];
      }
    }
    setInterval("slide();",1000);
    </script>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    All that would do is delay all of what I described for 1 second. And then it would still happen the same way.

    You clearly don't understand what it takes to make a slide show. Isn't it time to go find a really good one and use it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Tried but was not sure how to implement them.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by sherlockturtle View Post
    for (i=0; i<images.Length; i++)

    Javascript is case-sensitive. Length != length.

    There are a zillion slide show scripts out there - Google or search this forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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