Jump to content


Photo

Need help with While loops and frames


  • Please log in to reply
7 replies to this topic

#1 BreakBreak

BreakBreak
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 16 October 2006 - 02:40 PM

I'm trying to keep a page open and looping for testing purposes of a further project.

I have 4 frames

They are all running while loops with a sleep(1) in them

They are all doing the following:

$i = 2;
$z = 1;
while($i > 1){

echo "$z second <br>";
sleep(1);
$z++;
}

However, for some perculiar reason, three of them stop SOMETIMES after 20 or so seconds, whilst the one to the farthest right carries on.

What baffles me is that SOME of them only stop SOMETIMES.

Another confusion, once it ran with all four of them counting for over 30 minutes and showed no sign of stopping.

PHP is NOT in safe mode.


---------

Please help me as this is slowing me down hugely and i need to find a solution, fast.

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 02:57 PM

Try this:
for($i = 1; $i <= 20; $i++)
{
	echo "{$i} seconds\n";
	sleep(1);
}


#3 BreakBreak

BreakBreak
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 16 October 2006 - 02:59 PM

Is that not the exact same thing?

Note: I want this to be an infinite loop.

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 03:00 PM

Try this then:
while(sleep(1))
{
	$i++;
	echo "{$i} seconds\n";
}


#5 BreakBreak

BreakBreak
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 16 October 2006 - 03:28 PM

Doesn't work, avoids the loop.

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 16 October 2006 - 03:50 PM

try renaming your variables for each frame. Using $i for all frames may be causing problems.

$i1 for first frame
$i2 for second  and so on

Ray

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 04:25 PM

This works:
<?php
while(1)
{
	sleep(1);
	$i++;
	echo "{$i} seconds\n";
}
?>

Output (command-line):
daniel@daniel:~$ php /var/www/test.php
1 seconds
2 seconds
3 seconds
4 seconds
5 seconds
6 seconds
7 seconds
8 seconds
9 seconds

daniel@daniel:~$


#8 BreakBreak

BreakBreak
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 16 October 2006 - 06:06 PM

Yes, but (sleep1) didn't.

I have changed them to for(;;) loops now and they are working fine, i also got my server to extend the termination time.

Thanks for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users