想要在脚本执行中添加延迟是很常见的。
在 PHP 中,这可以使用不同的 sleep 函数来完成。
原生 sleep() 函数将在指定的时间内暂停脚本的执行(以秒为单位)。
也可以使用 time_nanosleep() 以纳秒为单位暂停执行,或者使用 usleep() 以微秒为单位暂停执行。
Milliseconds 没有本机函数,但我们可以创建自己的函数。
要睡眠十秒钟,我们只需在脚本中输入以下内容:
echo "This is the beginning\n"; sleep(10); echo "And this is the end\n";
最大执行时间
睡眠函数不计入脚本 max_execution_time。
一种测试方法是从浏览器运行以下脚本:
$starttime = time(); time_sleep_until(time()+60); //Do Stuff Here $finishtime = time(); header('Content-Type: text/plain; charset=UTF-8'); echo $finishtime-$starttime . " seconds\n\n"; echo 'max_execution_time: '. ini_get('max_execution_time') . "\n";
输出应该是 ~59 秒。
此外,从命令行运行的脚本没有最大执行时间。
日期:2020-06-02 22:17:35 来源:oir作者:oir