site/js/Cache.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-03-24 21:31:02 +00:00
export class InputFieldCache {
private typingTimer: ReturnType<typeof setTimeout>;
private doneTypingInterval: number = 1000;
constructor() {
this.cacheInputFields();
}
private async cacheInputFields() {
const inputFields = document.querySelectorAll('input');
console.debug(inputFields);
inputFields.forEach((input: HTMLInputElement) => {
input.addEventListener('input', () => {
clearTimeout(this.typingTimer);
this.typingTimer = setTimeout(() => {
console.debug(`${input.id}: ${input.value}`);
localStorage.setItem(input.id, input.value);
}, this.doneTypingInterval);
});
const cachedValue = localStorage.getItem(input.id);
if (cachedValue !== null) {
input.value = cachedValue;
}
});
}
async clearCache() {
const inputFields = document.querySelectorAll('input');
inputFields.forEach((input: HTMLInputElement) => {
localStorage.removeItem(input.id);
});
}
}