elis Posted August 4, 2009 Share Posted August 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/ Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 sleep doesn't work the way you expect. PHP is server-side, so when you tell it to sleep, it will delay output to the client until it has finished running the script. ob_start is where I recommend you start to get what you are wanting. Quote Link to comment https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/#findComment-890560 Share on other sites More sharing options...
aschk Posted August 4, 2009 Share Posted August 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/#findComment-890591 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2009 Share Posted August 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/#findComment-890607 Share on other sites More sharing options...
aschk Posted August 4, 2009 Share Posted August 4, 2009 The OP has only asked for an answer to his question, which has been given. As to how she/he intends to utilise this information we don't know yet. I'm sure it will become apparent soon Quote Link to comment https://forums.phpfreaks.com/topic/168800-some-help-using-sleep/#findComment-890678 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.