« Swoole\Timer
Swoole\Timer::clear »

Swoole\Timer::after

(PECL swoole >= 1.9.0)

Swoole\Timer::after — Execute a function after a specified time

説明

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

Creates a one-shot timer, which is destroyed once its callback has run. Unlike sleep(), it does not block the current process.

パラメータ

ms

The delay 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 once the delay has elapsed. It is called as callback(mixed ...$params). Unlike Swoole\Timer::tick(), the timer ID is not passed to the callback.

params

Additional values passed to callback.

戻り値

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

例

例1 Swoole\Timer::after() example

<?php
Swoole\Timer::after(1000, function (string $name) {
    echo "Hello, $name\n";
}, "Swoole");
?>

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

Hello, Swoole
関連キーワード:
Swoole   Timer   after   callback   timer   Execute   specified   time   params   passed   clear   mixed   once   Unlike   delay   Hello   swoole   static   callable   Creates   shot   destroyed   sleep   block   current
関連する関数・ページ:
swoole-timer.clear   class.swoole-timer   swoole-timer.exists   swoole-timer.stats   swoole-timer.tick   swoole-timer.info