Show results while script is still executing

前端 未结 4 750
遇见更好的自我
遇见更好的自我 2020-11-28 06:16

Right now in order to see the results, I have to wait until the entire code is done executing. It hangs until it\'s complete and stays loading. Once it\'s finished it shows

相关标签:
4条回答
  • 2020-11-28 06:35

    I had to put both ob-flush and flush as shown in this example:

    for($i=10; $i > 0; $i--)
    {
        echo "$i ...";
        flush();
        ob_flush();
        sleep(1);
    }
    
    0 讨论(0)
  • 2020-11-28 06:42

    You can do that with output buffering. Turn on output buffering at the top of your script with ob_start(). That makes PHP to send no output to the browser. Instead its stored internally. Flush your output at any time with ob_flush(), and the content will be sent to the browser.
    But keep in mind that output buffering is influenced by many other factors. I think some versions of IIS will wait until the script is finished, ignoring output buffering. And some Antivirus software on client side (Was it Panda?) might wait until the page is fully loaded before passing it through to the browser.

    0 讨论(0)
  • 2020-11-28 06:49

    You can use output buffering like this:

    ob_start();
    
    echo('doing something...');
    
    // send to browser
    ob_flush();
    
    // ... do long running stuff
    echo('still going...');
    
    ob_flush();
    
    echo('done.');
    ob_end_flush(); 
    
    0 讨论(0)
  • 2020-11-28 06:52

    This one worked for me: (source)

    function output($str) {
        echo $str;
        ob_end_flush();
        ob_flush();
        flush();
        ob_start();
    }
    
    0 讨论(0)
提交回复
热议问题