Swoole\Timer::tick
(PECL swoole >= 1.9.0)
Set a repeating interval timer
Description
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.
Parameters
msThe interval in milliseconds. Must be greater than or equal to
1; a lower value emits anE_WARNINGand the call fails.callbackThe 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 inparams.paramsAdditional values passed to
callbackafter the timer ID.
Return Values
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.
Examples
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");
?>The above example will output:
tick #1 (job)
tick #2 (job)
tick #3 (job)