mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
36 lines
596 B
TypeScript
36 lines
596 B
TypeScript
|
|
|
|
export interface Debouncer {
|
|
debounce(callback: Function): void;
|
|
cancel(): void;
|
|
}
|
|
|
|
export class TimeoutDebouncer implements Debouncer {
|
|
private timer: number = null;
|
|
callback: Function;
|
|
|
|
constructor(public wait: number) { }
|
|
|
|
debounce(callback: Function) {
|
|
this.callback = callback;
|
|
this.schedule();
|
|
}
|
|
|
|
schedule() {
|
|
this.cancel();
|
|
if (this.wait <= 0) {
|
|
this.callback();
|
|
} else {
|
|
this.timer = setTimeout(this.callback, this.wait);
|
|
}
|
|
}
|
|
|
|
cancel() {
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
this.timer = null;
|
|
}
|
|
}
|
|
|
|
}
|