UNPKG

@benev/slate

Version:
40 lines 1.13 kB
export function repeating(milliseconds, fn) { let active = true; const execute = async () => { if (active) { await fn(); setTimeout(execute, milliseconds); } }; setTimeout(execute, milliseconds); return () => { active = false; }; } repeating.hz = (hertz, fn) => repeating(1000 / hertz, fn); ///////////////////////////////////////////////////// /** @deprecated use `repeat` instead */ export class Repeater { milliseconds; fn; active = true; constructor(milliseconds, fn) { this.milliseconds = milliseconds; this.fn = fn; 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, fn) { return new Repeater(milliseconds, fn); } /** @deprecated use `repeat.hz` instead */ repeater.hz = (hertz, fn) => repeater(1000 / hertz, fn); //# sourceMappingURL=repeater.js.map