Flushing HTML to browser while looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jeroen Oosterlaar
Forum Commoner
Posts: 37
Joined: Sun Nov 06, 2005 4:12 pm

Flushing HTML to browser while looping

Post by Jeroen Oosterlaar »

Hi,

I have a problem regarding flushing the output buffer to the browser while walking through a for-loop. I want to show a progress bar while my mailing list sending routine is executing. To keep things simple see the following simplified snippet:

Code: Select all

for($i = 0; $i < count($addresses); $i++)
{
    mail(...);
    echo ".";
    ob_flush();
    flush();
    sleep(1);
}
The strange thing is: when running the script (which, on average, takes about 1 minute), the output seems to be flushed when viewing the source code of the page during the execution, but the output is not being rendered to the screen by the browser.

What could be the problem and is there a solution?

Thanks in advance!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's your browser's choice to render or not. Since it knows the page isn't done loading, nor did an element finish, it will likely choose to not draw anything, as it can't calculate the dimensional requirements.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Firefox has an about:config option nglayout.initialpaint.delay, which "determines time in milliseconds to wait before an initial reflow attempt during page rendering." and set it to 0, it should speed rendering.
Post Reply