我想在我的代码中引入一些睡眠功能,以便我可以在指定的(时间或间隔)调用函数。
我想要的输出:
(1)在前5 sec
im called
(2)10 sec
(i,e 5sec + 5秒)
im called // at first iteration of loop
im called // at second iteration of loop
现在我正在使用
function curl_grab(){
echo "im called<br/>";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
但上面代码的问题是它一次抛出所有内容
喜欢这样
im called
im called
我一个接一个地想要它
这是demo:http://phpio.net/s/ggr
答案 0 :(得分:1)
如果您使用浏览器测试此代码,您将只能看到输出,只有完成php代码执行后才能看到输出。您可以在终端中执行此操作以查看发生时的输出。
将此代码写入test.php
function curl_grab(){
echo "im called\n";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
要在终端执行,您可以输入
$ php /path/to/test.php