namespace Leaf { export class Debouncer { constructor(public readonly option: { time: number maxTime: number }) { } previousEmit: number = 0 timer = null debounce(fn: Function) { clearTimeout(this.timer) if (Date.now() - this.previousEmit > this.option.maxTime) { this.previousEmit = Date.now() fn() return } this.timer = setTimeout(() => { this.previousEmit = Date.now() fn() }, this.option.time) } } export class GroupDebouncer { constructor(public readonly option: { time: number maxTime: number }) { } debouncers: { [name: string]: Debouncer } = {} debounce(name: string, fn: Function) { if (!this.debouncers[name]) { this.debouncers[name] = new Debouncer(this.option) } this.debouncers[name].debounce(fn) } } }