Hall of Famer Posted April 4, 2012 Share Posted April 4, 2012 Well PHP manual says that the script execution will be delayed by $x seconds if you write a codes like this: $x = 5; //5 secs sleep($x); Now I have this script in which I write the four lines to the screen one by one, each should be delayed by 5 secs after the last line is output to the screen. It does not work out at all... echo "Update setting 1:"; sleep(5); echo "Success! <br>Updating setting 2"; sleep(5); echo "Success! <br>Updating setting 3"; sleep(5); echo "Success! <br>Updating setting 4"; Instead, the entire script execution is delayed by 15 secs, with all four lines output at the same time. Why is this happening? Is there a way to fix it? Please help... Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/ Share on other sites More sharing options...
scootstah Posted April 4, 2012 Share Posted April 4, 2012 That's how sleep() works. Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334488 Share on other sites More sharing options...
litebearer Posted April 4, 2012 Share Posted April 4, 2012 sleep() is server side - perhaps consider ajax to get and display the content in a delayed manner Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334489 Share on other sites More sharing options...
samshel Posted April 4, 2012 Share Posted April 4, 2012 try using ob_start() and ob_flush(). ob_start(); echo "Update setting 1:"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 2"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 3"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 4"; ob_flush(); ob_flush flushed out the buffer contents. Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334493 Share on other sites More sharing options...
Hall of Famer Posted April 5, 2012 Author Share Posted April 5, 2012 try using ob_start() and ob_flush(). ob_start(); echo "Update setting 1:"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 2"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 3"; ob_flush(); sleep(5); echo "Success! <br>Updating setting 4"; ob_flush(); ob_flush flushed out the buffer contents. Thank you so much for this advice, I tried the codes but it did not work. I heard it from php.net that I need to add the line 'php_value output_buffering "0"' to my .htaccess file but it gave me a 500 internal server error when I did that. Is there any prerequisite I should do before using ob_start() and ob_flush()? Anyway I know this task can be completed with AJAX but I am looking for a way to use PHP only since I dont understand AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334547 Share on other sites More sharing options...
kicken Posted April 5, 2012 Share Posted April 5, 2012 What your attempting to do cannot be done reliably with just PHP. The problem is there could be any number of buffers between your script and the client browser which you have no control over. Your browser itself could buffer the data before rendering it to the screen. Calling both flush and ob_flush after your output operations is pretty much about as good as you can do. If you do that and it still does not work then you must have some other buffer somewhere preventing it from working. Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334556 Share on other sites More sharing options...
Hall of Famer Posted April 5, 2012 Author Share Posted April 5, 2012 What your attempting to do cannot be done reliably with just PHP. The problem is there could be any number of buffers between your script and the client browser which you have no control over. Your browser itself could buffer the data before rendering it to the screen. Calling both flush and ob_flush after your output operations is pretty much about as good as you can do. If you do that and it still does not work then you must have some other buffer somewhere preventing it from working. I see, thanks for letting me know about it. I did consider other possibilities, the most reliable one is AJAX but I dont know how to write AJAX codes. Can anyone of you please be generous enough to write me a something like a function outputtext($text, $timedelay) that uses AJAX to outout each line of text to the screen after 1-3 secs of delay? I do not really expect such luxury, but I appreciate it if someone does it. For now I will just keep testing PHP codes and see if by any chance I can figure out a way to do it with PHP only. Quote Link to comment https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/#findComment-1334594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.