bnt Posted March 5, 2009 Share Posted March 5, 2009 I need to display some text (using echo) and then wait a few seconds before continuing. As an example... <?php echo "test199"; flush(); sleep(5); ?> Running this results in a delay of 5 seconds and then displaying the "test99". I have tried flush() and other output buffering functions but nothing seems to work. Any help on this trivial problem would be greatly appreciated!!! Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted March 5, 2009 Share Posted March 5, 2009 The sleep(); function has to come before the echo statment. The parser cascades the file from top to bottom as it reads it. - like CSS. Try this: <?php sleep(5); echo "test199"; ?> flush(); shouldn't be necessary. Quote Link to comment Share on other sites More sharing options...
bnt Posted March 5, 2009 Author Share Posted March 5, 2009 Per your suggestion, I put sleep before echo. No difference. It sleeps 5 seconds before displaying the echo. I need to display the echo then wait 5 seconds and continue <?php sleep(5); echo "test299"; #some other code ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2009 Share Posted March 5, 2009 Php, the web server, and the browser all can do buffering, compression, and minimum-length block/buffering that prevent incremental output from being sent to or rendered by the browser. Web servers and browsers are not designed for one request for a page to be displayed in increments. Why do you want to do this? If all the text is known at the time the page is requested but you want to reveal it incrementally, you need to send it all at once and use javascript to reveal it the way you want. If the text is real time status that you want to update, you need to use AJAX to periodically fetch the status from the web server and display it on the page. Quote Link to comment Share on other sites More sharing options...
bnt Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks for your response. I am rather new to this and see what you are getting at... but have no idea how to implement it! All I want to do is display some text, wait a number of seconds, and continue with my script. I cannot figure how javascript could make this work. If you could point me to some code, I would really appreciate it!!! Quote Link to comment Share on other sites More sharing options...
bnt Posted March 5, 2009 Author Share Posted March 5, 2009 I would really appreciate some help on this!!! Thanks Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 11, 2009 Share Posted March 11, 2009 If you are using a web host, make sure they allow this, where as my host, doesn't and if I want to enable it, I need to contact them. PHP functions flush()' date=' ob_flush(), and ob_implicit_flush() will have no apparent effect on DreamHost. For performance reasons on a DreamHost shared host, output is buffered at a higher level than PHP (mod_gzip) and so these commands do not have any visible effect. If you need unbuffered output, you must contact Tech Support to request mod_gzip be disabled for your site. [/quote'] Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted March 28, 2009 Share Posted March 28, 2009 you could try this: <?php ob_implicit_flush(true); echo "test199"; sleep(5); ?> Quote Link to comment 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.