« Swoole\Timer::stats
Swoole\WebSocket\Frame »

Swoole\Timer::tick

(PECL swoole >= 1.9.0)

Swoole\Timer::tick — Set a repeating interval timer

説明

public static function Swoole\Timer::tick(int $ms, callable $callback, mixed ...$params): int|false

Sets a timer that triggers every ms milliseconds. Unlike a timer created with Swoole\Timer::after(), it keeps triggering until it is removed with Swoole\Timer::clear().

パラメータ

ms

The interval in milliseconds. Must be greater than or equal to 1; a lower value emits an E_WARNING and the call fails.

callback

The function to execute on every tick. It is called as callback(int $timer_id, mixed ...$params): the ID of the timer is passed as the first argument, followed by the values given in params.

params

Additional values passed to callback after the timer ID.

戻り値

Returns the timer ID, which can be passed to Swoole\Timer::clear(). Returns false if the timer could not be created, in particular when ms is less than 1.

例

例1 Swoole\Timer::tick() example

<?php
$ticks = 0;
Swoole\Timer::tick(1000, function (int $timer_id, string $label) use (&$ticks) {
    echo "tick #", ++$ticks, " ($label)\n";
    if ($ticks === 3) {
        Swoole\Timer::clear($timer_id);
    }
}, "job");
?>

上の例の出力は以下となります。

tick #1 (job)
tick #2 (job)
tick #3 (job)
関連キーワード:
timer   Swoole   Timer   tick   interval   callback   params   ticks   repeating   clear   passed   mixed   milliseconds   created   after   values   label   stats   WebSocket   Frame   swoole   static   callable   Sets   triggers
関連する関数・ページ:
swoole-timer.clear   swoole-timer.info   swoole-timer.stats   swoole-timer.exists   swoole-timer.after   class.swoole-timer