Home  >  Article  >  PHP Framework  >  How to set timer in swoole

How to set timer in swoole

(*-*)浩
(*-*)浩Original
2019-12-06 09:50:462410browse

How to set timer in swoole

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn