Sleep inside a while loop, help please!

Discussion in 'PHP' started by Crayz, Oct 5, 2007.

  1. #1
    Basically I want to loop through and open a new url in each iFrame, but between each iFrame there must be a 2.2 second delay. But it's not doing that, what it's doing is delaying, then loading all of the iFrames at once.

    Any suggestions?

    <?php
    
    $url = "www.site.com/id=";
    
    $id = 100000005;
    $delay = 2.2;
    
    $x = 5;
    
    while($x > 0) {
    
           Sleep($delay);
           echo '<iframe name="FRAMENAME" src="'.$url . $id.'" width="100" height="280" style="border: 10 solid #0415FB" scrolling="no" allowautotransparency=true></iframe>';
           $x--;
           $friendid++;
    
    }
    
    ?>
    Code (markup):
    Thanks!
     
    Crayz, Oct 5, 2007 IP
  2. imagize

    imagize Peon

    Messages:
    48
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to flush PHP's output buffers after each iteration to get the effect you desire.

    <?php
    
    $delay = 1;
    // max number of iterations
    $it = 10;
    
    while($it > 0)
    {
    	echo $it;
    	flush();
    	sleep($delay);
    	$it--;
    }
    
    ?>
    PHP:
    Tested on Centos4/PHP5.

    If that does not work for you try

    <?php
    
    $delay = 1;
    // max number of iterations
    $it = 10;
    
    while($it > 0)
    {
    	echo $it;
    	ob_flush();
    	flush();
    	sleep($delay);
    	$it--;
    }
    
    ?>
    PHP:
     
    imagize, Oct 5, 2007 IP
  3. scriptman

    scriptman Peon

    Messages:
    175
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can also use usleep($microseconds) for more accurate delays.
     
    scriptman, Oct 5, 2007 IP