Jump to content

Delaying an echo();


DanC

Recommended Posts

Hi,

 

I am quite new to PHP and I am trying to use a sleep() function to delay the response of sending text. E.g:

"Hello world 1"
// Wait 3 seconds
"Hello world 2"
// Wait 3 seconds
"Hello world 3"
// etc...

 

I have tried to use sleep(3) but it just ends up either slowing down the whole script or showing all echo's after 3 seconds. Here is what I am doing.

 

<?php
echo "Hello world 1";
sleep(3);
echo "Hello world 2";
sleep(3);
echo "Hello world 3";
?>

 

Any advice?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/172435-delaying-an-echo/
Share on other sites

Please read the following information in the flush section of the documentation as to why you might get this to work on one server/browser combination and not another -

 

  Quote
flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

 

Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

 

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

 

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the </table> tag of the outermost table is seen.

 

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

 

Web servers and browsers ARE NOT DESIGNED to send and display the output from a single HTTP request this way. It ties up server resources and more importantly ties up one of the available connections to the server.

Link to comment
https://forums.phpfreaks.com/topic/172435-delaying-an-echo/#findComment-909144
Share on other sites

  Quote

Hi,

 

I am quite new to PHP and I am trying to use a sleep() function to delay the response of sending text. E.g:

"Hello world 1"
// Wait 3 seconds
"Hello world 2"
// Wait 3 seconds
"Hello world 3"
// etc...

 

I have tried to use sleep(3) but it just ends up either slowing down the whole script or showing all echo's after 3 seconds. Here is what I am doing.

 

<?php
echo "Hello world 1";
sleep(3);
echo "Hello world 2";
sleep(3);
echo "Hello world 3";
?>

 

Any advice?

 

Thanks.

<?php
ob_start();
for($i=1;$i<=3;$i++){
echo"Hello World $i<br/>";
ob_flush();
flush();
sleep(3);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/172435-delaying-an-echo/#findComment-909197
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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