Jump to content

Problems using sleep() function


bnt

Recommended Posts

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!!!

Link to comment
Share on other sites

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

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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']
Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.