大网数据广告活动
PHP 语言与运行手册

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

· 52CE 我爱测

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

Swoole\Timer::after

(PECL swoole >= 1.9.0)

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

Description

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.

Parameters

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.

Return Values

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.

Examples

Example #1 Swoole\Timer::after() example

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

The above example will output:

Hello, Swoole

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。