Set timer
swoole provides JavaScript-like setInterval/setTimeout asynchronous high-precision timer with millisecond-level granularity.
It is also very simple to use. (Recommended learning: swoole video tutorial)
Program code
//每隔2000ms触发一次 swoole_timer_tick(2000, function ($timer_id) { echo "tick-2000ms\n"; }); //3000ms后执行此函数 swoole_timer_after(3000, function () { echo "after 3000ms.\n"; });
The swoole_timer_tick function is equivalent to setInterval and is continuously triggered
The swoole_timer_after function is equivalent to setTimeout, which is only triggered once at the agreed time.
The swoole_timer_tick and swoole_timer_after functions will return an integer, indicating the ID of the timer.
You can use swoole_timer_clear to clear this timing. The parameter is the timer ID
The above is the detailed content of How to set timer in swoole. For more information, please follow other related articles on the PHP Chinese website!