View Full Version : delaying a while loop
Jacobb123
12-09-2007, 11:42 PM
Is there a way to pause a while loop for a couple of seconds before doing the next loop?
I have a script that I would like it to wait 5 seconds before executing the next loop but not sure how to do this.
CFMaBiSmAd
12-10-2007, 12:20 AM
May I ask why you want to do this.
Because of all the buffering and compression that web servers and browsers do before a web page is sent and rendered, using a delay in a loop to attempt to control how something is displayed can only be made to work under limited conditions and just ties up resources on the server and keeps a http connection open.
If you want to control how something is displayed in the browser, you need to use javascript or make it a movie/flash.
If this is for doing something like delaying between sending emails, the same comments apply, it ties up server resources and keeps a http connection open. In this case you should either cause a page to refresh itself to trigger sending more emails or use a cron job to execute the script at timed intervals until all the emails have been sent.
Jacobb123
12-10-2007, 12:44 AM
It was for sending emails. I wasn't sure if there was a different way to do it besides CRON jobs. But is seems this would probably be the better way. Thanks
kbluhm
12-10-2007, 01:09 AM
For your future reference, the functions sleep() and usleep() will delay script execution.
sleep( 5 ); // delay for 5 seconds
usleep( 500 ); // delay for 0.5 seconds
aedrin
12-10-2007, 04:56 PM
The best way is to send a batch of emails, set a session variable with the current index and then send a meta tag refresh to refresh in 1 second. Or however long you need it to be. This will keep reloading the page until you are done. And will not trigger the usual spam measures.
For your future reference, the functions sleep() and usleep() will delay script execution.
This doesn't take care of the script execution limit (which is usually 60 seconds).
psykx
12-10-2007, 05:35 PM
as long as you gave the page no actual content you could use header( location: ) to reload the page
kbluhm
12-11-2007, 04:05 PM
This doesn't take care of the script execution limit (which is usually 60 seconds).
He didn't ask about extending the execution time, he asked about delaying a while loop.
aedrin
12-11-2007, 04:10 PM
Correct, but I wanted to alert the user that if this solution was used, such an issue could arise.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.