debounce input change

This commit is contained in:
iamBadgers
2025-08-17 12:57:39 -07:00
parent 468bf24214
commit ec98fb9975
3 changed files with 26 additions and 7 deletions

View File

@@ -2,4 +2,5 @@
export const MODULE_ID = "rush-character-archive-foundry-module";
export const URI_SETTING_NAME = "uri_setting";
export const API_KEY_NAME = "api_key";
export const API_KEY_NAME = "api_key";

View File

@@ -54,7 +54,7 @@ export class CharacterArchiver extends Application {
html
.find(".character-handle")
.on("change", this.handleChange.bind(this));
.on("input", this.handleChange.bind(this));
}
private async archiveButton() {
@@ -80,11 +80,16 @@ export class CharacterArchiver extends Application {
this.client.createCharacter(request);
}
timeout: NodeJS.Timeout | undefined;
private async handleChange(e: Event) {
console.log(e.target);
const target = e.target as HTMLTextAreaElement;
this.data.handle = target.value;
console.log(this.data.handle);
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
console.log(e.target);
const target = e.target as HTMLTextAreaElement;
this.data.handle = target.value;
console.log(this.data.handle);
}, 500);
}
}

View File

@@ -2,4 +2,17 @@
export const MODULE_ID = "rush-character-archive-foundry-module";
export const URI_SETTING_NAME = "uri_setting";
export const API_KEY_NAME = "api_key";
export const API_KEY_NAME = "api_key";
export function debounce<P extends any[]>(
callback: (...args: P) => any,
time: number
): (...args: P) => void {
let timer: NodeJS.Timeout | undefined;
return function (this: any, ...args: P) {
clearTimeout(timer);
timer = setTimeout(() => callback.apply(this, args), time);
return timer;
}
}