Results 1 to 4 of 4

Thread: Whats going wrong?

  1. #1
    Join Date
    Nov 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Whats going wrong?

    Basically i am trying to get this script to cycle through the images in the array in a loop with a timed delay

    The script cycles the images through fine but will not reset back to the first image I just cant figure out whats going wrong and its driving me mad

    Please help!!

    Code:
    <html>
    
    <head>
    <title>Testing Page</title>
    
    <script type="text/javascript">
    
    var myCounter = 0;
    var myArray = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'];
    var arraylen = myArray.length;
    
    function changepic()   
    { 
    document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">'; 
    myCounter++;
    }
    
    function changepic2()   
    {
    var myCounter = 0;
    document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">'; 
    }
    
    function alertme()   
    {
    if(arraylen > myCounter){ 
    intervalID = setInterval(changepic, 5000);
    }else{
    intervalID = setInterval(changepic2, 5000);
    }
    }
    
    </script> 
    
    </head>
    
    <body bgcolor='#ffffff' onload="alertme();">
    
    <div id='test'>
    <img src="image1.jpg">
    </div>
    
    </body>
    
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Without correcting a lot of things that are non-standard about your demo code:

    Code:
    <html>
    
    <head>
    <title>Testing Page</title>
    
    <script type="text/javascript">
    
    var myCounter = 0;
    var myArray = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'];
    var arraylen = myArray.length;
    
    function changepic()   
    {
    if(arraylen > myCounter){
    document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">'; 
    myCounter++;
    }
    else {
    myCounter = 0;
    changepic();
    }
    }
    
    function alertme()   
    {
    intervalID = setInterval(changepic, 5000);
    }
    
    </script> 
    
    </head>
    
    <body bgcolor='#ffffff' onload="alertme();">
    
    <div id='test'>
    <img src="image1.jpg">
    </div>
    
    </body>
    
    </html>
    should (untested) take care of it. The basic problem in your version is that:

    Code:
    function alertme()   
    {
    if(arraylen > myCounter){ 
    intervalID = setInterval(changepic, 5000);
    }else{
    intervalID = setInterval(changepic2, 5000);
    }
    only runs once, and that even if it ran continually and started setting various different intervals, things would fairly quickly get all jumbled up with several intervals playing out concurrently, some even simultaneously.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    scooby545 (11-20-2008)

  4. #3
    Join Date
    Nov 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Thankyou it all makes sense now lol

    kinda new to javascript lol

    thanks

  5. #4
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Scooby 545

    I suggest you check out jquery.com It makes JavaScript life so much easier. There're many plugins which are available to offer you the functions you need. Maybe http://sorgalla.com/projects/jcarousel/#Examples ?

    Regards
    ggerri

Bookmarks

Posting Permissions

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