Timed loop in php

mastero picture mastero · May 22, 2012 · Viewed 9.2k times · Source

I just want to print a counting from 1 to 10 at an interval of 10 sec between each integer.

eg.

$i=10; //Time delay    
for($j=1;$j<11;$j++)  
{  
    echo $j;  
    //do something to delay the execution by $i seconds  
}

I have tried everything including flush(), ob_flush(), ob_implicit_flush() but all i get is a frozen screen untill the whole time is executed.

Answer

Tim picture Tim · May 22, 2012

http://php.net/manual/en/function.sleep.php

The sleep function will interrupt execution of your script.

But have you considered using Javascript for something like this? Your script may reach maximum execution time, and will be hogging resources on the server. Use the client's resources instead!