1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Execute...Output...Pause...Resume... Is this possible?

Discussion in 'PHP' started by AHA7, May 19, 2007.

  1. #1
    Hello,

    I am trying to write a PHP script that executes gradually as seperated by a time pause between its different parts. Here's a working example:

    <?php
        set_time_limit( 0 ); //unset the execution time defaut limit (30 sec.)
    
        //Do...(php code)
        echo "Done part 1<br>\n";
    
        sleep(10); //pause for 10 sec.
        //Do...(php code)
        echo "Done part 2<br>\n";
    
        sleep(10); //pause for 10 sec.
        //Do...(php code)
        echo "Done part 3<br>\n";
        .
        .
        .
        .
        .
        .
    ?> 
    PHP:
    The code above does the following: execute>>pause for 10 sec.>>resume execution>>pause for 10 sec.>>resume......>>OUTPUT AFTER THE END OF THE SCRIPT IS REACHED

    But I want it to do the following: execute>>OUTPUT>>pause for 10 sec.>>resume execution>>OUTPUT>>pause for 10 sec.>>resume execution>>OUTPUT......

    I do know why PHP was called so and I do know what that means, but is there anyway to make the script pause for a while, output all the code executed before it paused and before the script ends (while it is processing the script), then continue this way until the end of the script...
     
    AHA7, May 19, 2007 IP
  2. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Yes you can, check out these very cool and useful functions: php.net/manual/ref.outcontrol.php

    :)
     
    wing, May 19, 2007 IP
    coderbari and AHA7 like this.
  3. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the useful link!
    Rep added! ;)
    Hey, this is my 100th post :cool:
     
    AHA7, May 19, 2007 IP
  4. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It may be that with so little output actually being written, apache and/or tcp/ip will wait to fill a packet before sending your data. So effectively it comes all at once. It would be more reliable to simulate delays with Javascript:

    <html>
    <head>
    <script language="Javascript" type="text/javascript">
    function reveal() {
      setTimeout("document.getElementById('part2').style.display = '';", 3000);
      setTimeout("document.getElementById('part3').style.display = '';", 6000);
    }
    </script>
    </head>
    
    <body onload="reveal();">
    
      <span id="part1"> Done part 1<br></span>
      <span id="part2" style="display: none;"> Done part 2<br></span>
      <span id="part3" style="display: none;"> Done part 3<br></span>
    
    </body>
    </html>
    HTML:
     
    lemaitre, May 19, 2007 IP
  5. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's a useful tip, but I want to execute other PHP codes in the different parts (not only echo's) and the output of each part would be much more than just "Done part #".
     
    AHA7, May 19, 2007 IP
  6. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you generate enough output during each phase of the program it will probably just work ;) This happens naturally whenever you write a script that has delays on the backend.
     
    lemaitre, May 19, 2007 IP
  7. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Yea wing's reference was so effective.this forum is giving me a lot of useful things to learn :)
     
    coderbari, May 19, 2007 IP