PHP 是否有类似于 setTimeout() (JavaScript) 的函数?
这个问题已经说明了一切 - 是否有一个函数与 PHP 的 JavaScript 函数 setTimeout() 具有相同的功能?我搜索了 php.net,但似乎找不到任何...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
无法延迟执行当前脚本中的部分代码。这也没有多大意义,因为 PHP 脚本的处理完全发生在服务器端,您只会延迟脚本的整体执行。有
sleep()
但它只会将进程暂停一段时间。当然,您可以使用 cron 作业等安排 PHP 脚本在特定时间运行。
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.
有
sleep
函数,它将脚本暂停一段确定的时间。另请参阅
usleep
、time_nanosleep
和time_sleep_until
< /a>.There's the
sleep
function, which pauses the script for a determined amount of time.See also
usleep
,time_nanosleep
andtime_sleep_until
.PHP 不是事件驱动的,因此 setTimeout 没有多大意义。您当然可以模仿它,事实上,有人编写了 计时器类 你可以使用。但在您开始在 PHP 的服务器端以这种方式进行编程之前,我会小心谨慎。
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.
关于 PHP 中的计时器,我想注意以下几点:
1) PHP 中的计时器在长时间运行的脚本(守护进程,也许还有 CLI 脚本)中使用时才有意义。因此,如果您不开发此类应用程序,那么您就不需要计时器。
2) 计时器可以是阻塞的,也可以是非阻塞的。如果您使用
sleep()
,那么它是一个阻塞计时器,因为您的脚本只会冻结指定的时间时间。对于许多任务来说,阻塞计时器是可以的。例如,每 10 秒发送一次统计信息。可以阻止脚本:
3) 非阻塞计时器仅在事件驱动的应用程序中才有意义,例如 websocket-server。在此类应用程序中,事件可能随时发生(例如传入连接),因此您不得使用
sleep()
阻止您的应用程序(显然)。为此,有一些事件循环库,例如 reactphp/event-loop,它允许您以非阻塞方式处理多个流,并且还具有计时器/间隔功能。
4) PHP 中的非阻塞超时是可能的。
可以通过
stream_select()
带有超时参数的函数(参见 reactphp 中的实现方式/event-loop StreamSelectLoop::run())。5) 有一些 PHP 扩展,例如
libevent
、libev
、event
,它们允许计时器实现(如果您想要硬核)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:
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)不是真的,但你可以尝试 刻度计数功能 。
Not really, but you could try the tick count function.
http://php.net/manual/en/class.evtimer.php可能是您正在寻找的,您可以在设置的时间间隔内调用一个函数,类似于 javascript 中的 setInterval 。它是一个 pecl 扩展,如果您有 whm/cpanel,您可以通过 pecl 软件/扩展安装程序页面轻松安装它。
我没有注意到这个问题是从2010年开始的,evtimer类是在2012-2013年开始编码的。因此,作为对旧问题的更新,现在有一个类可以执行类似于 javascripts settimeout/setinterval 的操作。
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.
警告:您应该注意,虽然
sleep
命令可以使 PHP 进程挂起或“休眠”一段给定的时间,但您通常会在用户界面。由于 PHP 是一种服务器端语言,只需将其执行输出(通常以 HTML 的形式)写入 Web 服务器响应:以这种方式使用 sleep 通常只会停止或延迟响应。
话虽如此,睡眠确实有实际用途。延迟执行可用于实现退避方案,例如在连接失败后重试请求时。一般来说,如果您需要在 PHP 中使用 setTimeout,那么您可能做错了什么。
解决方案:如果您仍然想在 PHP 中实现 setTimeout,请明确回答您的问题:考虑 setTimeout 有两个参数,一个代表要运行的函数,另一个代表时间量(以毫秒为单位)。下面的代码实际上可以满足您问题中的要求:
上述代码的输出将延迟三秒,然后是以下输出:
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:
The output of the above code will be three seconds of delay, followed by the following output:
如果您需要在执行一些 php 代码后执行操作,您可以使用 echo 来完成,
因此在客户端(浏览器)中一段时间后,您可以执行其他操作,例如重定向到另一个 php 脚本或 echo 警报
if you need to make an action after you execute some php code you can do it with an echo
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
PHP 版本中有一个
Generator
类可用 > 5.5 提供了一个名为yield
的功能,可以帮助您暂停并继续下一个功能。generator-example.php
OUTPUT
如果您想在第一个
yield
之后执行代码,请添加这些行现在您将获得输出
如果您仔细观察 setTimeout() 创建了一个事件循环。
在 PHP 中,有很多库,例如 amphp 是一种流行的,它提供事件循环来异步执行代码。
Javascript 代码段
使用 Amphp 将上述 Javascript 代码段转换为 PHP
There is a
Generator
class available in PHP version > 5.5 which provides a function calledyield
that helps you pause and continue to next function.generator-example.php
OUTPUT
If you want to execute the code after the first
yield
you add these lineNow you will get output
If you minutely see the setTimeout() creates an event loop.
In PHP there are many libraries out there E.g amphp is a popular one that provides event loop to execute code asynchronously.
Javascript snippet
Converting above Javascript snippet to PHP using Amphp
如果您想要运行代码 30 秒并在几秒钟内显示,例如发送 OTP 并再次重新发送代码。
if you want run code for 30 seconds and display in seconds like sending OTP and resend code again.