The question sort of says it all - is there a function which does the same as the JavaScript function setTimeout() for PHP? I've searched php.net, and I can't seem to find any...
+3
A:
There's the sleep
function, which pauses the script for a determined amount of time.
See also usleep
, time_nanosleep
and time_sleep_until
.
Artefacto
2010-08-08 18:01:56
+5
A:
No, there is not - timed events wouldn't make much sense, either, as the processing of a PHP script takes place entirely on server side. There is sleep()
but that will simply halt the process for a certain time.
Maybe explain in some detail what you want to do - I'm sure somebody will be able to come up with an alternative suggestion.
Pekka
2010-08-08 18:02:37
It is a little hard to explain - what I wanted was exactly to pause the script :) Basically what I want to do is to "prank" one of my friends. I've made an input where you type in your name and when you submit a chatroom is simulated and to make it more realistic I want the messages from the stranger, my friend is "talking to", to appear one by one if you know what I mean :)
Latze
2010-08-08 18:06:01
Timed events don't make sense because the script runs on server side? I'm not following the argument. Certainly they are less useful in PHP due to the absence of concurrency, but there's value in having a script that does something after a specific amount of time or on a certain instant.
Artefacto
2010-08-08 18:08:31
@Latze that sounds like something you would want to do using JavaScript on client side, doesn't it?
Pekka
2010-08-08 18:08:40
@Latze Then `sleep` or, for more fine-grained control `usleep`, is an adequate solution.
Artefacto
2010-08-08 18:09:22
I aggree, Pekka. I do :) I actually don't know why I'm doing it in PHP...
Latze
2010-08-08 18:10:43
@Artefacto I see absolutely no use for *timed* events, because it's impossible to predict the script's flow and execution speed, so there is nothing to time against. *Event* events (like "after each mysql query" or "before outputting the first byte of data") might be useful if concurrency were possible, but *timing* makes sense only when there's human interaction as far as I can see.
Pekka
2010-08-08 18:11:11
@Pekka Well, a timed event would be useful e.g. in a streaming Comet connection to send messages to the client on a specific time. The lack of concurrency could be worked around with in a [discrete event simulation](http://en.wikipedia.org/wiki/Discrete_event_simulation) manner, by keeping a list of future events and on each step of the loop extract the next event and pause the script until its time. And don't forget PHP is not used exclusively to serve webpages, this could be useful also for command line programs.
Artefacto
2010-08-08 18:18:45
@Artefacto good points - hadn't thought of those.
Pekka
2010-08-08 18:20:05
+5
A:
PHP isn't event driven, so a setTimeout doesn't make much sense. You can certainly mimic it and in fact, someone has written a Timer class you could use. But I would be careful before you start programming in this way on the server side in PHP.
ars
2010-08-08 18:06:55