Posted by SeanC (SeanC), 8 November 2004I'm writing a script using a while statement which needs to pause for 45 seconds everytime it's been through the loop - is this possible in PHP and if so what command do I need to use.
Posted by admin (Graham Ellis), 9 November 2004It's possible but you're very likely going to end up solving your immediate question and raising others.
The sleep function in PHP delays program execution for a given number of seconds:
is the straightforward answer to your question
a) Buffering. If your script is buffering its output, then information written to the buffer will NOT be flushed out (automatically) when you get to the sleep. In other words, if you have a while loop that sleep 5 times 45 seconds, you may not see any output at all for 225 seconds
b) Timeouts. Your script will have limited execution time, and your user's browsers may give up after a certain period.
I have personally used sleep to pause a script for a couple of seconds between reading from other URLs internally (to avoid any hint of being accused of making a denial of service attack), and to ensure that buffers are flushed / files written before kicking off something that reads files I've just written. It's also useful in standalone PHP programs.
Sean, I'm *not* saying that you shouldn't use sleep - I would need a wider view that your question gives - but *if* you're generating multiple pages from a single script then you should consider structuring your script to use a refresh metatag and genarate a completely new page each time.
<meta http-equiv="Refresh" content="10; ">
We have an example using this 10 second pause at http://www.wellho.co.uk/slideshow. This example is really for use on our inhouse network though; the graphics are huge (they rescale to fit the window so must be ultra high res) and you'll need a good "very" broadband connection for it to be effective. It's a "dream" on our 3 Mbit link.
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho