Results 1 to 4 of 4

Thread: sleep()

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question sleep()

    Hi,

    I have a situation, I found problem regarding sleep(). I like to display seconds from 60 to 0. First it will display 60, then after sleep() of 1 second it will display 59 and so on until it reach to 0. But it will display the all 60 numbers at a time, whereas I like to display one number at a time with 1 second interval.

    Here is my code. Please help. Thanks a lot.

    PHP Code:
    <?php 

    for($i=60;$i>=0;$i--)
    {
        echo 
    $i;
        
    sleep(1);
    }

    ?>

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: sleep()

    Call ob_flush before calling sleep. This will flush the response stream and ensure the latest number is displayed on the client side.

    Edit: make that @ob_flush() to suppress a notice if output buffering happens to be disabled.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: sleep()

    Thanks for reply penagate. But this does not make any difference. Following is my rectified code. Please help. Thanks a lot.

    PHP Code:
    <?php
    for($i=60;$i>=0;$i--)
    {
        echo 
    $i;
        
    ob_flush();
        
    sleep(1);
    }
    ?>

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: sleep()

    I use flush() instead of ob_flush() and this is working. But I would like see the number in decreasing order one at a time. But here numbers are displayed like this 60595857..... Please check it out. Thanks a lot.

    PHP Code:
    <?php

    for($i=60;$i>=0;$i--)
    {
        echo 
    $i;
        
    flush();
        
    sleep(1);
    }

    ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width