PHP: time_sleep_until()

shawntc

Member
Messages
70
Reaction score
0
Points
6
(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.
 

KryptosV2

New Member
Messages
24
Reaction score
1
Points
0
Spock-zq-mr-spock-5589461-579-434.jpg


Whenever you can, use client side script.
 

shawntc

Member
Messages
70
Reaction score
0
Points
6
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.
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
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.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Not understanding the usage of Spock.
It's probably in joking response to the last line of your first 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.
 
Top