Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/260366-php-sleep-function-wont-work/
Share on other sites

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.

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.

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.

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.

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.