...

View Full Version : Escaping infinite loop



juliankramer
07-24-2006, 04:47 PM
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!

Kirl
07-24-2006, 04:54 PM
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...

Kor
07-24-2006, 05:27 PM
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... :D But I don't like it :rolleyes:

switch(myvar){
case somevariable
...do something...;
break;
case somevariable
...do something...;
break;
.........
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum