PHP sleep 延迟

标签 php for-loop delayed-execution timedelay

在 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/

相关文章:

java - 如何安排不受计算机挂起影响的定时事件

java - 从 PHP 脚本调用 Java(或 python 或 perl)

php - Allowed memory size of x exhausted error 使用 Yii 安装 PHPUnit

javascript - 通过 for 循环中的拼接从数组中删除项目

stream - 延迟评估期间的内存

iphone - Objective-C - 在给定时间/日期执行功能

php - 如何在一行显示 4 <li> 而在另一行显示其他 4 <li>

php - 如何将同一个订单号的产品价格相加?在 codeigniter 中加入查询

javascript - 如何自动重新加载网页

r - 有条件地从 R 中的字符串模式中提取数字