set_interval

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

Parameters

ParametersTypeRequiredDefault valueDescription
fnFnisNoneA BT function that is executed repeatedly.
delay_msIntNo1Interval in milliseconds; if it is less than 1, it will be treated as 1.

Return value

TypeDescription
TimerTimer handle, you can call cancel() to stop subsequent triggering.

Example

Notes

  • The callback can declare that the first parameter receives the current Timer object to facilitate calling self.cancel() inside the callback.
  • When the callback does not declare parameters, the extra self parameters 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.