Documentation
¶
Overview ¶
Package timers brings JavaScript's timing functions to Go, with the same names and argument order: SetTimeout and SetInterval (with ClearTimeout and ClearInterval to cancel), plus lodash's Debounce and Throttle.
// JS: const id = setTimeout(() => save(), 200); clearTimeout(id); t := timers.SetTimeout(save, 200*time.Millisecond) timers.ClearTimeout(t)
Like their JavaScript counterparts, these take no context: the returned handle (or the cancel function from Debounce/Throttle) is how you stop them.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearInterval ¶
func ClearInterval(iv *Interval)
ClearInterval stops a running Interval, mirroring clearInterval(id). It is idempotent, and a nil handle is a no-op.
func ClearTimeout ¶
func ClearTimeout(t *Timeout)
ClearTimeout cancels a pending Timeout if it has not fired yet, mirroring clearTimeout(id). A nil handle is a no-op.
func Debounce ¶
Debounce returns a debounced wrapper around fn: each call to debounced postpones fn until d has passed with no further calls, so a burst of calls runs fn once, at the end. It mirrors lodash's _.debounce. cancel discards any pending call.
Types ¶
type Interval ¶
type Interval struct {
// contains filtered or unexported fields
}
Interval is the handle returned by SetInterval. Pass it to ClearInterval to stop the repeating calls.
func SetInterval ¶
SetInterval calls fn every d, mirroring JavaScript's setInterval(fn, ms). The calls run on a dedicated goroutine, one after another; a slow fn delays the next tick rather than overlapping. Stop it with ClearInterval.
type Timeout ¶
type Timeout struct {
// contains filtered or unexported fields
}
Timeout is the handle returned by SetTimeout, the analogue of the timer id setTimeout returns in JavaScript. Pass it to ClearTimeout to cancel.
func SetTimeout ¶
SetTimeout runs fn once after d elapses, mirroring JavaScript's setTimeout(fn, ms). fn runs on its own goroutine. The returned Timeout can be passed to ClearTimeout to cancel before it fires.