php - 在选定的时间后停止执行循环

我想在选定的时间后停止执行循环。意味着循环时它将尝试执行一个参数并花费很长时间然后它将完成工作并转到下一个参数。例如:

for ($i=2 ; $i <= 100 ; $i++)

这里假设会循环3,4,5,6,7,8,9,10,在11中会执行很长时间。所以我只想在(即 10 秒)之后停止它并选择下一个选项,我的意思是 12、13、14... 等等。 那么,我该怎么做。我设置了这个 set_time_limit(10); ,但它在这里不起作用。

最佳答案

您可以使用 microtime()测量耗时。请注意,这只会在迭代完成后中断。

$start = microtime(true);
$limit = 10;  // Seconds

for ($i=2 ; $i <= 100 ; $i++) {
  if (microtime(true) - $start >= $limit) {
    break;
  }
}

https://stackoverflow.com/questions/13195464/

相关文章:

jasper-reports - 如何删除碧 Jade 报告中的重复行

emacs - GNU Emacs 中的前段

python - 在网格管理器中使用带有 Tkinter 的输入框?

python - 如何在python字符串中找到相反的字符?

heroku - 让应用程序确定它是否在 Heroku 上运行

apache - 同一路径的多个 Apache Location 指令

sql-server-2008 - SQL中错误跟踪系统的数据库设计

arduino - avrdude.exe : invalid file format ' ' in

git - 将分支 merge 到 master 上的新子目录中

vim-plugin - 在插入模式下使用 vundle 绑定(bind) ctrl-h 以在 vi