PDA

View Full Version : looping...


NinjaTurtle
07-15-2002, 02:33 AM
i have 50 times of looping with a counter, can i break it to 2 pages to do accumulation. this is an example:

i have a for loop,

for ( var i = 0; i < 50; i++) {
document.write "Number now is :" &i&"<BR>"
}

from the above example the result will be listed 50 lines of
Number now is :1
Number now is :2
Number now is :3
Number now is :4
Number now is :5
.
. until 50 times.
.
Number now is :50

but what i want is
when the loop run until i=25, it will stop for while and shown a link or button "Continue>>", when user click on it, the loop will continue runs from i=26 until 50? Just like PAUSE the loop then play back the loop. can javascript do that?

x_goose_x
07-15-2002, 03:39 AM
This any good?

<html>

<head>

<script>

num = 50; //total length
brk = 2; //amout of breaks

part = 0;
z = num/brk;

function next() {
if (part<brk) {
more = "";
for ( y=0; y<z; y++) {
x = y+part*z;
more += "Number now is :"+x+"<BR>";
}
document.body.innerHTML += more;
part += 1;
}
}

</script>

</head>

<body onload="next();">

<a href="javascript: next();">more</a><br>

</body>

</html>

If you need any changes just ask.

NinjaTurtle
07-15-2002, 07:03 AM
but how if i got 1,000 loops? it will take quit long time to process, it until my PC hang. so any solution? some more inside the loop, still hav many sub loop to run.... how?

RadarBob
07-16-2002, 12:44 AM
How about this?

function ImSoLoopy() {
var numOfLoops = 1000;
var RestPeriod = 25;

for (var i=0 i< numOfLoops; i++) {
if (i % RestPeriod == 0) {
alert ("I'm resting every" + RestPeriod + "loops\r" +
"Press OK when ready to continue");
}
document.write "Number is now: " i;
}
} // function ImSoLoopy()


So what the heck is this?: if (i % RestPeriod == 0)
It does a division and returns the remainer. If the remainder is zero then "i" is a multiple of "RestPeriod".

but how if i got 1,000 loops? it will take quit long time to process,

No. It won't.

NinjaTurtle
07-16-2002, 03:34 AM
i get few error ... 1 is the semicolon, and other e1 i dunno what is the error..>!!??!!

glenngv
07-16-2002, 03:41 AM
Originally posted by NinjaTurtle
i get few error ... 1 is the semicolon, and other e1 i dunno what is the error..>!!??!!

RadarBob forgot the + sign and parenthesis:

document.write("Number is now: " + i);