Can anyone see my error?

Discussion in 'JavaScript' started by Jen-, Oct 22, 2006.

  1. #1
    Hello, I am trying this one instead I gave up on the other. This should work with only slight modification because I got most of it from a working script. Can someone please tell me where my error is? The image will not appear, I have a path to a real image. If you're wondering why I have the content inside a bracket is because I will be adding more to it later.

    This goes in the body.
    <BODY onload="changeImg();">
    <img id="img" border="0"/>

    This goes in the external, or it can be tested on the same page.
    var content = new Array();

    content[0] = ["http://www.website.com/3.jpg"];
    content[1] = ["http://www.website.com/6.jpg"];

    var counter = 0;
    function changeImg()
    { document.getElementById("img").src = content[counter-1][0];
    counter++; if (counter == content.length)
    counter = 0; } setInterval("changeImg()", 40000);
     
    Jen-, Oct 22, 2006 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I think the JS console makes the error quite clear.
    counter-1 equals -1
     
    Logic Ali, Oct 23, 2006 IP
  3. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please explain, I still don't see what your trying to tell me, thanks.
     
    Jen-, Oct 23, 2006 IP
  4. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That wasn't it but got it thanks.
     
    Jen-, Oct 23, 2006 IP
  5. sxg

    sxg Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I thought -1 would cause the error as well... Just curious. What was the error?
     
    sxg, Oct 29, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    It starts with counter being set to -1, which does not exist in the array. This should do it.
    
    <script type="text/javascript">
    
    var content = new Array();
    var counter = 0;
    
    content[0] = "http://www.website.com/3.jpg";
    content[1] = "http://www.website.com/6.jpg";
    
    function changeImg()
    {
    	document.getElementById("img").src = content[counter];
    	counter++;
    
    	if (counter == content.length)
    		counter = 0; 
    }
    
    setInterval("changeImg()", 40000);
    </script>
    
    Code (markup):
     
    nico_swd, Nov 1, 2006 IP
  7. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thats preety nice code my friend, but if all the images have the same prefix then sum it up (http://www.website.com/)
     
    Morishani, Nov 1, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Yeah, DRY (Don't repeat yourself). I just edited quick the original script. And the URLs are more of examples anyway.
     
    nico_swd, Nov 1, 2006 IP