Results 1 to 6 of 6

Thread: how does php sleep function works

  1. #1
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how does php sleep function works

    Hi


    If i run the below script then both the dates are displayed after delay of 15 seconds.

    But i think 1st date should get displayed instantly and 2nd should get display after 15 seconds.


    But its not working like that ??


    Code:
    <?php
    echo date('H:i:s');
    sleep(15);
    echo "<br>";
    echo date('H:i:s');
    ?>
    Vineet

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there vinpkl,

    your reasoning is slightly flawed.

    You make a call to your server, the php script is run and when
    this is fully completed, the result is returned to your browser.

    coothead
    ~ the original bald headed old fart ~

  3. #3
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    PHP's output stream is normally buffered and nothing is sent to the browser until the script terminates. If you want to see the result of the first "echo" statement immediately, you need to flush the buffer before the "sleep()" statement is executed.

    Code:
    <?php
    echo date('H:i:s');
    flush();
    sleep(15);
    echo "<br>";
    echo date('H:i:s');
    ?>

  4. #4
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Styx

    flush() also didnt made any difference.

    The page is displayed with both dates after 15 seconds delay.

    first date is not displayed immediately.

    vineet

  5. #5
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by coothead View Post
    Hi there vinpkl,

    your reasoning is slightly flawed.

    You make a call to your server, the php script is run and when
    this is fully completed, the result is returned to your browser.

    coothead
    Thats what is happening. the page is displayed with both dates after 15 seconds.

    Vineet

  6. #6
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default


    One call to the server, one reply.
    ~ the original bald headed old fart ~

Similar Threads

  1. Replies: 7
    Last Post: 06-18-2013, 03:48 PM
  2. Resolved JS screensaver / sleep mode for website?
    By davelf in forum Looking for such a script or service
    Replies: 4
    Last Post: 07-27-2011, 03:05 AM
  3. Vista Sleep Problem
    By Johnnymushio in forum Computer hardware and software
    Replies: 19
    Last Post: 11-18-2007, 08:09 PM
  4. sleep() pauses scripts on all pages :(
    By noobster in forum PHP
    Replies: 3
    Last Post: 03-30-2007, 10:56 AM
  5. Replies: 5
    Last Post: 09-30-2006, 07:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •