set_interval
set_interval
Function
Registers a fixed-delay repeating callback and returns a Timer object immediately. The next trigger time is calculated only after each round of callback execution is completed, so the same interval will not re-enter, and events will not accumulate due to slow callbacks.
Syntax
timer = set_interval(fn, delay_ms)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| fn | Fn | is | None | A BT function that is executed repeatedly. |
| delay_ms | Int | No | 1 | Interval in milliseconds; if it is less than 1, it will be treated as 1. |
Return value
| Type | Description |
|---|---|
| Timer | Timer handle, you can call cancel() to stop subsequent triggering. |
Example
count = 0 set_interval(fn(self) { count++ if count >= 3 { self.cancel() } // Output: current count println(count) }, 10)
Notes
- The callback can declare that the first parameter receives the current
Timerobject to facilitate callingself.cancel()inside the callback. - When the callback does not declare parameters, the extra
selfparameters will be ignored. - If an error is thrown in the callback, the error will be recorded, and subsequent intervals will continue to execute by default; if you need to stop, please capture the error in the callback and cancel.
- Timers cannot be created in the context of a web request.