+ Reply to Thread
Results 1 to 5 of 5

Thread: PHP: time_sleep_until()

  1. #1
    shawntc is offline x10Hosting Member shawntc is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    62

    PHP: time_sleep_until()

    (Grrr I despise PHP's use of the underscore. I so prefer it to be timeSleepUntil() like how they're set up in my native script, JavaScript.)

    Say I have this code:
    Code:
    <html>
    <head>
    <title>time_sleep_until</title>
    </head>
    <body>
    <?php
    echo "Loaded";
    time_sleep_until(time()+1);
    echo "1 second";
    time_sleep_until(time()+1);
    echo "2 seconds";
    time_sleep_until(time()+3);
    echo "Woke up";
    ?>
    </body>
    </html>
    What I want this to do is load the page and display "Loaded", then one second later display "1 second" and so on. But instead it appears to delay the loading of the entire page and then displays them all at once. The effect I am desiring is similar to using setTimeout() in JavaScript, except the processing is done server side. Am I correct in assuming that I would have to use Ajax in this case? Also, I want the script to commence the time_sleep_until()'s after the page has been loaded. Finally, my third queston is: if I call a time_sleep_until() and then leave that page, will the time_sleep_until() command continue to delay until that time?

    Sorry, I understand this post is anything but logical.
    For God so loved the world that he gave is one and only son, that whoever believes in him shall not perish but have eternal life. - John 3:16
    MY SITE!

  2. #2
    KryptosV2 is offline x10Hosting Member KryptosV2 is an unknown quantity at this point
    Join Date
    May 2010
    Posts
    24

    Re: PHP: time_sleep_until()



    Whenever you can, use client side script.

  3. #3
    shawntc is offline x10Hosting Member shawntc is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    62

    Re: PHP: time_sleep_until()

    Not understanding the usage of Spock. I'll use JavaScript whenever I can, but I'd still like to be able to queue up a series of things to be done server-side and I suppose time_sleep_until() is what I need.
    For God so loved the world that he gave is one and only son, that whoever believes in him shall not perish but have eternal life. - John 3:16
    MY SITE!

  4. #4
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: PHP: time_sleep_until()

    Quote Originally Posted by shawntc View Post
    Not understanding the usage of Spock. I'll use JavaScript whenever I can, but I'd still like to be able to queue up a series of things to be done server-side and I suppose time_sleep_until() is what I need.
    Try using flush() each time you want to push something to the screen.

    So like this:

    Code:
    TimeconsumingThing();
    echo "Done with step 1!";
    flush();
    TimeconsumingThing2();
    echo "Done with step 2!";
    Would do TimeconsumingThing(), flush "Done with step 1!" to the browser, -then- start working on the second part. While it's still loading, your browser -should- show the "Done with step 1!" part while the page continues processing and doing whatever you're doing.

    I'm not sure that'll do exactly what you need it to, but give it a shot.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP: time_sleep_until()

    Quote Originally Posted by shawntc View Post
    Not understanding the usage of Spock.
    It's probably in joking response to the last line of your first post.

    Quote Originally Posted by shawntc View Post
    I'll use JavaScript whenever I can, but I'd still like to be able to queue up a series of things to be done server-side and I suppose time_sleep_until() is what I need.
    Sleep functions block execution of the script/process/thread. They won't do what you need. PHP doesn't really support concurrency, which is what you're looking for. If any of the sub-process functions, such as system() or popen, are enabled (which they aren't, at least not on the free hosts), you can get concurrency by launching additional processes. The closest thing to setTimeout is to use signals, if pcntl is enabled (which it probably isn't), you'd set an alarm handler with pcntl_signal() and schedule an alarm signal with pcntl_alarm. Note that there's no direct equivalent to setInterval, which is closer to what you need. Note also that this probably isn't the most usable solution. You could also use fopen (if allow_url_fopen is enabled) or hack together with curl to request the worker script server-side, since both support asynchronous calls to get data from a URL. Other than that, you'll have to rely on the asynchronous nature of HTTP for concurrency like any AJAX based app.

    If there are specific steps your script goes through, simply output a progress update and flush(), as Livewire suggests. You could even output script elements that update a progress meter.

    Remember that scripts have a time-limit of 30 seconds. If you need to do much time-consuming work, it's better to break it up over multiple requests and use AJAX.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

LinkBacks (?)

  1. 05-28-2010, 04:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers