SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sleep method question

    I know using a sleep method to do delay messages isnt the main purpose for this function but I need to do it for a demo. Basically when someone clicks submit on the fill out form it pops up a javascript window. What it is suppose to do is echo out Processing....

    Then 5 seconds later it then does the rest. For testing I did:

    PHP Code:
     
    echo 'Processing...';
    flush();
     
    sleep(5);
     
    //do rest; 
    Here is the problem. The first time it loads the javascript window comes up but the page doesnt show Processing.... until the whole page loads. So at the same time it shows Processing and the outcome of the code. But if I click refresh in that javascript window it works as it should. Showing Processing, waiting 5 seconds and then doing the rest.

    I read the manual and it stated something about browser cache/buffer and to flush after the last table or something to that nature. I tried it and it still is an issue. I tried it on just a regular html page and the same issue occurs. Any ideas?

    Silly

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Try this
    PHP Code:
    ob_implicit_flush();
    echo 
    'Processing...';
    sleep(5);
    //do rest 
    But alternatively you can use Javascript to achieve the same means. Clicky

  3. #3
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    
    ob_implicit_flush(); 
    echo 'Processing...'; 
    sleep(5); 
    
    echo '5..'; 
    sleep(1);  
    echo '4..'; 
    sleep(1); 
    echo '3..'; 
    sleep(1); 
    echo '2..'; 
    sleep(1); 
    echo '1..'; 
    sleep(1); 
    echo 'Start !'; 
    
    ?>
    This didn't work for me. It just the 10 seconds and then printed it all at once.

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    This is what the PHP manual has to say, and it highlights the point that flush/ob_flush() isn't really a practical solution.
    Quote Originally Posted by PHP Manual
    flush() has no effect on the buffering scheme of your webserver or the browser on the client side.

    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.
    If you want a simple loading screen, I'd suggest a JS answer


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
  •