set_timeout
set_timeout
Function
Registers a one-time deferred callback and returns a Timer object immediately. After the callback expires, it will be executed once by the single-threaded event loop of the VM to which it belongs, and the callback return value will be ignored.
Syntax
timer = set_timeout(fn, delay_ms)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| fn | Fn | Yes | None | Delayed execution of the BT function. |
| delay_ms | Int | No | 0 | Delay in milliseconds; negative numbers are treated as 0. |
Return value
| Type | Description |
|---|---|
| Timer | Timer handle, you can call cancel() to cancel the callback that has not yet been triggered. |
Example
fired = false timer = set_timeout(fn() { fired = true }, 100) // Output: false print fired
Notes
-
set_timeout()will not execute the callback synchronously; whendelay_msis 0, it will be delivered to the event queue as soon as possible. - CLI scripts with an active timeout will remain in progress until the callback execution completes or is cancelled.
- A timer cannot be created in the Web request context, and the VM will be released after the request ends.
- For the method of canceling the timer, see Timer.cancel.