Sleep function in a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gordicron
Forum Newbie
Posts: 10
Joined: Thu Oct 13, 2005 3:38 pm

Sleep function in a loop

Post by Gordicron »

Hi,

Well it looks like I have a new problem! :lol:

I've written some PHP code which reads a text file containing some URLs into an array. Some Javascript is then called upon to open a new browser window and display the URLs in this window, in succession. The idea is that I can do this with a delay - i.e. each URL will display for something like say 30 seconds and then open the new one. Here's my code:

Code: Select all

<?php
$urls_array = file('process.txt');
$last_displayed_url = $_GET['lurl'];
$new_url = $urls_array[(array_search($last_displayed_url, $urls_array)+1)];

foreach ($urls_array as $url) { 
$url = trim($url);
echo "<script language=Javascript>";
echo "window.open('$url','test');";
echo "</script>";
}

?>
I've tested the code and it works. The problem is that it works too quick! So, I tried to add a sleep command like so:

Code: Select all

...rest of code...

foreach ($urls_array as $url) { 
$url = trim($url);
echo "<script language=Javascript>";
echo "window.open('$url','test');";
echo "</script>";
sleep(30); //Added a sleep command to the loop here
}
As I understand it, that should delay the execution for 30 seconds... Problem is, it doesn't! :( The webpages are definitely being opened in succession as I see them open rapidly in the browser window that gets opened but for some reason the sleep(30) command seems to be being ignored.

Does anyone know why this may be or if there is a better way to introduce a delay in a loop like this? Everything works else works fine except for this one little thing.

- Gordon
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

good try but it does somtin like micoseconds with usleep.

Code: Select all

usleep(30000000);
that will do 30 seconds
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Add

Code: Select all

flush();
right before the sleep function. PHP will tend to stack the output and flush it to the browser once the script execution has finished. The flush command will force whatever output is in the buffer to the browser before each 30 second delay.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Some browsers still cache the output and display it in one piece. Another problem I had on win32 was the flush() function didn't work properly. Here's what solved the problem:

Code: Select all

/* Make sure IE flushes */
  echo str_repeat(' ', 300);

  /*
    Flush content just in case
    Curiously, both ob_flush() and flush()
    are needed for this to work...
  */
  ob_flush();
  flush();
User avatar
Moocat
Forum Contributor
Posts: 143
Joined: Wed Oct 12, 2005 9:28 am
Location: USA

Post by Moocat »

I am not the same person but I did try out his code he posted and it does appear (at least in Firefox) that you NEED the flush() command. Without it, it loads forever (well, for quite a while) and then just spits it all out. With it, it still takes a long time to load, but then goes through the code bit by bit and puts out every 3 (I changed it from 30) seconds. Is there some way to speed up the preload, because I only had one small bit of code and even that took about 3-5 seconds to load (just load the first time).

Code: Select all

$url = 0;
while ($url < 7) {
	echo $url;
	sleep(3); //Added a sleep command to the loop here
	flush();
$url++;
}
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Moocat wrote:I am not the same person but I did try out his code he posted and it does appear (at least in Firefox) that you NEED the flush() command. Without it, it loads forever (well, for quite a while) and then just spits it all out. With it, it still takes a long time to load, but then goes through the code bit by bit and puts out every 3 (I changed it from 30) seconds. Is there some way to speed up the preload, because I only had one small bit of code and even that took about 3-5 seconds to load (just load the first time).

Code: Select all

$url = 0;
while ($url < 7) {
	echo $url;
	sleep(3); //Added a sleep command to the loop here
	flush();
$url++;
}
You could maybe try a flush() before your while loop to make sure that the browser gets something.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't know if this could be the source of your problem, but some (if not all) browsers require a certain amount of data to be sent before it will load the page

throw some garbage data before the loop and see if that does anything for you

Code: Select all

echo "garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage";

// loop here
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

scrotaye wrote:

Code: Select all

echo "garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage";
...compare to...
foobar wrote:

Code: Select all

/* Make sure IE flushes */
  echo str_repeat(' ', 300);
Last edited by foobar on Tue Nov 15, 2005 2:40 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yes but you didn't point that out to him, perhaps he didn't know what it was and overlooked it :P i was explaining
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

scrotaye wrote:yes but you didn't point that out to him, perhaps he didn't know what it was and overlooked it :P i was explaining
foobar wrote:

Code: Select all

/* Make sure IE flushes */
;)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

scrotaye wrote:I don't know if this could be the source of your problem, but some (if not all) browsers require a certain amount of data to be sent before it will load the page

throw some garbage data before the loop and see if that does anything for you
vs
foobar wrote:

Code: Select all

/* Make sure IE flushes */
my description's better, I win!! :lol:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

scrotaye wrote:
scrotaye wrote:I don't know if this could be the source of your problem, but some (if not all) browsers require a certain amount of data to be sent before it will load the page

throw some garbage data before the loop and see if that does anything for you
vs
foobar wrote:

Code: Select all

/* Make sure IE flushes */
my description's better, I win!! :lol:
:P

Smartass...
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

foobar wrote:
scrotaye wrote:
foobar wrote:

Code: Select all

/* Make sure IE flushes */
Not flushing is grounds for a divorce in some states.. :-D
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

trukfixer wrote:
foobar wrote:
scrotaye wrote:
Not flushing is grounds for a divorce in some states.. :-D
Buahahahahaha!! :D
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

foobar wrote:
Moocat wrote:I am not the same person but I did try out his code he posted and it does appear (at least in Firefox) that you NEED the flush() command. Without it, it loads forever (well, for quite a while) and then just spits it all out. With it, it still takes a long time to load, but then goes through the code bit by bit and puts out every 3 (I changed it from 30) seconds. Is there some way to speed up the preload, because I only had one small bit of code and even that took about 3-5 seconds to load (just load the first time).

Code: Select all

$url = 0;
while ($url < 7) {
	echo $url;
	sleep(3); //Added a sleep command to the loop here
	flush();
$url++;
}
You could maybe try a flush() before your while loop to make sure that the browser gets something.
Actually the flush function should be immediately BEFORE the sleep function. This will force all output before the program goes into sleep mode.
Post Reply