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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Escaping infinite loop

    Hi,

    I would like to create an image "slideshow" for which I am using the following loop:

    <script type="text/javascript">

    intImage = 22;
    function swapImage() {
    if (intImage >= 22) {
    intImage=1; }
    else {
    intImage++;
    document.getElementById("IMG1").src = "images/P"+intImage+".jpg";
    }
    }

    intImage = 1;
    function swapImageback() {
    if (intImage <= 1) {
    intImage=22; }
    else {
    intImage--;
    document.getElementById("IMG1").src = "images/P"+intImage+".jpg";
    }
    }

    </script>

    The problem with the loop is that it's infinite, which I understand is generally not a very clever solution. In this particular case I would like to escape the loop with P20 and then link to a new page.

    Any advice on this would be very much appreciated.

    Thanks!

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    I believe you can escape any kind of loop by using break; within the loop. For example: if ( condition ) { break;}

    Not completely sure, but give it a try...
    Last edited by Kirl; 07-24-2006 at 03:56 PM.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You must use conditional loops to do that

    1. use an "if" stoped limit:

    if(var i=0;i<5;i++){
    alert(i)
    }
    2. use a "while" limit (which is actually to be used mainly in Boolean affairs)

    var stop=5
    var i=0;
    while(i<stop){
    alert(i);
    i++;
    }

    3. Use the "switch/case" conditional loop... But I don't like it

    switch(myvar){
    case somevariable
    ...do something...
    ;
    break;
    case somevariable
    ...do something...
    ;
    break;
    .........
    }
    Last edited by Kor; 07-24-2006 at 04:34 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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