Jump to content

Recommended Posts

I'm trying to use sleep(); to delay my program for a few seconds. I haven't used it before so I may be misunderstanding how it works.

 

What I wanted was a message to print, a pause in the script entirely, and then a second message to print.

However what I'm getting is a pause and then both messages print.

 

<?php
if(isset($_POST['submit'])) {

echo 'message 1';
sleep(5);
echo '<br> message 2';

}
?>

 

what I'm receiving is a five second pause after hitting submit on the form and then both messages print at the same time. I wanted a pause between them.

Link to comment
https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/
Share on other sites

I discovered something recently, which might give you what you're after.

 

Stick the following at the top of your code:

<?
ob_implicit_flush(true);
ob_end_flush();

// ... rest of code

?>

 

I have however noticed a rather disturbing lag in the closing of the PHP/Apache/TCP connection, so I would approach this with caution.

Web servers and browsers were not designed to incrementally output content from a single http request and have it rendered in "real time" in the browser. The web server, php, and the browser are/can do buffering, compression, and minimum block length buffering. You need to satisfy or disable all these things in order to get this to work on any server/browser combination. So, while you might get this to work where you have administrative access to the server and it is your browser, as soon as you attempt this on a public server that is not under your control and with a visitor's browser, it is likely to fail.

 

If you expect to have dynamically changing content and have it displayed in near real time in the browser and have it work independently of the server or of the browser, you need to make repeated requests for the information using Ajax.

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.