我如何每秒运行一个php脚本? [英] How do i run a php script every second?

查看:63
本文介绍了我如何每秒运行一个php脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写在线游戏.现在,我必须检查事件是否发生(检查数据库中的时间戳),并根据该事件执行一些操作.我必须每秒检查一次事件.

I am currently writing a online game. Now I have to check if an event happen (checking timestamp in database) and depending on that execute some actions. I have to check for an event every second.

我想使用cronjob,但是使用cron,您只能每分钟运行一次脚本.

I wanted to use a cronjob but with cron you can run a script only every minute.

我的想法是在我的php脚本中使用cron并循环60次.但是我认为这不是最好的解决方案.

My idea was to use cron and loop 60 times in my php script. But I think this isn't the best solution.

那么每秒运行脚本的最佳方法是什么?

So whats the best way to run a script every second?

推荐答案

我在寻找更好的解决方案,但似乎我的第一个想法(使用简洁的代码)是最好的解决方案.

I searched for a better solution but it seems that my first idea, with clean code, is the best solution.

这是我当前的代码:

<?php
set_time_limit(60);
$start = time();

for ($i = 0; $i < 59; ++$i) {
    // Do whatever you want here
    time_sleep_until($start + $i + 1);
}
?>

这篇关于我如何每秒运行一个php脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆