Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Pausing

Posted by SeanC (SeanC), 8 November 2004
I'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.

Thanks,

Sean.

Posted by admin (Graham Ellis), 9 November 2004
It'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:
              sleep(45);
is the straightforward answer to your question

BUT ...

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.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho