在 PHP 中,我想在循环的每次迭代中设置一定秒数的延迟。
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
//sleep for 3 seconds
}
我该怎么做?
最佳答案
使用 PHP sleep()
函数。 http://php.net/manual/en/function.sleep.php
这会在给定的秒数内停止执行下一个循环。所以像这样
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
sleep(3); // this should halt for 3 seconds for every loop
}
关于PHP sleep 延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414605/