Is there a function similar to setTimeout() (JavaScript) for PHP?

徘徊边缘 提交于 2019-12-17 16:44:08

问题


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...


回答1:


There is no way to delay execution of part of the code of in the current script. It wouldn't make much sense, either, as the processing of a PHP script takes place entirely on server side and you would just delay the overall execution of the script. There is sleep() but that will simply halt the process for a certain time.

You can, of course, schedule a PHP script to run at a specific time using cron jobs and the like.




回答2:


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.




回答3:


There's the sleep function, which pauses the script for a determined amount of time.

See also usleep, time_nanosleep and time_sleep_until.




回答4:


Not really, but you could try the tick count function.




回答5:


A few things I'd like to note about timers in PHP:

1) Timers in PHP make sense when used in long-running scripts (daemons and, maybe, in CLI scripts). So if you're not developing that kind of application, then you don't need timers.

2) Timers can be blocking and non-blocking. If you're using sleep(), then it's a blocking timer, because your script just freezes for a specified amount of time. For many tasks blocking timers are fine. For example, sending statistics every 10 seconds. It's ok to block the script:

while (true) {
    sendStat();
    sleep(10);
}

3) Non-blocking timers make sense only in event driven apps, like websocket-server. In such applications an event can occur at any time (e.g incoming connection), so you must not block your app with sleep() (obviously). For this purposes there are event-loop libraries, like reactphp/event-loop, which allows you to handle multiple streams in a non-blocking fashion and also has timer/ interval feature.

4) Non-blocking timeouts in PHP are possible. It can be implemented by means of stream_select() function with timeout parameter (see how it's implemented in reactphp/event-loop StreamSelectLoop::run()).

5) There are PHP extensions like libevent, libev, event which allow timers implementation (if you want to go hardcore)




回答6:


Warning: You should note that while the sleep command can make a PHP process hang, or "sleep" for a given amount of time, you'd generally implement visual delays within the user interface.

Since PHP is a server side language, merely writing its execution output (generally in the form of HTML) to a web server response: using sleep in this fashion will generally just stall or delay the response.

With that being said, sleep does have practical purposes. Delaying execution can be used to implement back off schemes, such as when retrying a request after a failed connection. Generally speaking, if you need to use a setTimeout in PHP, you're probably doing something wrong.

Solution: If you still want to implement setTimeout in PHP, to answer your question explicitly: Consider that setTimeout possesses two parameters, one which represents the function to run, and the other which represents the amount of time (in milliseconds). The following code would actually meet the requirements in your question:

<?php
// Build the setTimeout function.
// This is the important part.
function setTimeout($fn, $timeout){
    // sleep for $timeout milliseconds.
    sleep(($timeout/1000));
    $fn();
}

// Some example function we want to run.
$someFunctionToExecute = function() {
    echo 'The function executed!';
}

// This will run the function after a 3 second sleep.
// We're using the functional property of first-class functions
// to pass the function that we wish to execute.
setTimeout($someFunctionToExecute, 3000);
?>

The output of the above code will be three seconds of delay, followed by the following output:

The function executed!




回答7:


http://php.net/manual/en/class.evtimer.php is probably what you are looking for, you can have a function called during set intervals, similar to setInterval in javascript. it is a pecl extension, if you have whm/cpanel you can easily install it through the pecl software/extension installer page.

i hadn't noticed this question is from 2010 and the evtimer class started to be coded in 2012-2013. so as an update to an old question, there is now a class that can do this similar to javascripts settimeout/setinterval.




回答8:


if you need to make an action after you execute some php code you can do it with an echo

 echo "Success.... <script>setTimeout(function(){alert('Hello')}, 3000);</script>";

so after a time in the client(browser) you can do something else, like a redirect to another php script for example or echo an alert



来源:https://stackoverflow.com/questions/3435418/is-there-a-function-similar-to-settimeout-javascript-for-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!