export function repeating(milliseconds: number, fn: () => (void | Promise)) { let active = true const execute = async() => { if (active) { await fn() setTimeout(execute, milliseconds) } } setTimeout(execute, milliseconds) return () => { active = false } } repeating.hz = (hertz: number, fn: () => Promise) => repeating(1000 / hertz, fn) ///////////////////////////////////////////////////// /** @deprecated use `repeat` instead */ export class Repeater { active = true constructor(public milliseconds: number, public fn: () => Promise) { this.execute() } async execute() { if (this.active === true) { await this.fn() setTimeout(() => this.execute(), this.milliseconds) } } stop() { this.active = false } } /** @deprecated use `repeat` instead */ export function repeater(milliseconds: number, fn: () => Promise) { return new Repeater(milliseconds, fn) } /** @deprecated use `repeat.hz` instead */ repeater.hz = (hertz: number, fn: () => Promise) => repeater(1000 / hertz, fn)