PHP循环没有中断
我使用php循环来了解任何文件更改。如果文件发生更改,我将尝试停止循环。但在我的情况下,循环继续,而不是停止PHP循环没有中断,php,laravel,laravel-4,Php,Laravel,Laravel 4,我使用php循环来了解任何文件更改。如果文件发生更改,我将尝试停止循环。但在我的情况下,循环继续,而不是停止 Route::get('api.chat.buffer/{job_id}',function($job_id){ $award= DB::table('job_awards')->where('job_id',$job_id)->first(); $dirname = base_path().'/files/chat/'; $file
Route::get('api.chat.buffer/{job_id}',function($job_id){
$award= DB::table('job_awards')->where('job_id',$job_id)->first();
$dirname = base_path().'/files/chat/';
$filename = $award->client_id.'_'.$award->user_id.'.timelog.log';
$i =1;
for($z=0;$z<=20;$z++){
$current_file_time = filemtime($dirname.$filename);
if($_GET['timestamp']<$current_file_time){
echo json_encode(array('status'=>'success','data'=>$current_file_time,'node'=>1)); die;
break;
}
sleep(1); // this should halt for 3 seconds for every loop
}
echo json_encode(array('status'=>'success','data'=>$current_file_time,'node'=>0));
die;
});
Route::get('api.chat.buffer/{job\u id}',函数($job\u id){
$award=DB::table('job_awards')->其中('job_id',$job_id)->first();
$dirname=base_path()。/files/chat/;
$filename=$award->client_id.“'.$award->user_id.'.timelog.log';
$i=1;
对于($z=0;$z$current_file_time,'node'=>1));die;
打破
}
睡眠(1);//每次循环应停止3秒
}
echo json_编码(数组('status'=>'success','data'=>$current_file_time,'node'=>0));
死亡
});
我正在创建聊天脚本,并在文件更改时跟踪更改
谢谢您能在for循环中使用sleep功能吗?。你试过了吗。尝试分配要中断的循环语句的数量;在这种情况下,它将中断1您可以在for循环中使用sleep函数吗?。你试过了吗。尝试分配要中断的循环语句的数量;在这种情况下,它将中断1执行了这一行
echo json\u编码(数组('status'=>'success','data'=>$current\u file\u time,'node'=>1));死亡代码>是否执行?那你为什么在里面加了die
。。。就连条件都符合。。但不是。。它一直在运行..on node=>0最后一行尝试在if语句之前添加echo$\u GET['timestamp']
和echo$current\u file\u time
以查看是否满足此条件?在if
中,只需编写echo hello代码>(仅此注释)并检查是否正在打印?我认为如果在任何情况下,数组([current_file_time]=>1469986444[get]=>1469985108)中都没有您的逻辑,这是这一行的输出echo json_encode(数组('status'=>'success','data'=>$current_file_time,'node'=>1));死亡代码>是否执行?那你为什么在里面加了die
。。。就连条件都符合。。但不是。。它一直在运行..on node=>0最后一行尝试在if语句之前添加echo$\u GET['timestamp']
和echo$current\u file\u time
以查看是否满足此条件?在if
中,只需编写echo hello代码>(仅此注释)并检查是否正在打印?我认为,如果在任何情况下阵列([当前文件时间]=>1469986444[获取]=>1469985108)这里的输出条件不令人满意,则您的逻辑永远不会进入内部。每次。。它提供相同的filemtime。。而不是给出新的filemtime..$current\u file\u time=filemtime($dirname.$filename);我不知道。。但在最后一行。。这是我写的。。如果($\u GET['timestamp']我不知道您是如何传递时间戳-格式-。请执行并打印r、var\u dump或echo,以查看您将$current\u file\u time与$\u GET['timestamp'进行比较的情况有何问题条件不满足..每次..它都给出相同的filemtime..而不是给出新的filemtime..$current\u file\u time=filemtime($dirname.$filename);我不知道..但在最后一行..我写了这个..如果($\u GET['timestamp'),它会工作吗我不知道您是如何传递时间戳-格式-。请执行并打印\r、var\u dump或echo,查看您将$current\u file\u time与$GET['timestamp'进行比较的情况有何问题