Allow cancel both by debounced and original functions

This commit is contained in:
Cohee 2024-07-22 22:33:48 +03:00
parent 17dc3fa4b5
commit 4de51087bc
1 changed files with 7 additions and 3 deletions

View File

@ -285,16 +285,20 @@ const debounceMap = new WeakMap();
*/
export function debounce(func, timeout = debounce_timeout.standard) {
let timer;
return (...args) => {
let fn = (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
debounceMap.set(func, timer);
debounceMap.set(fn, timer);
};
return fn;
}
/**
* Cancels a scheduled debounced function. Does nothing if the function is not debounced or not scheduled.
* @param {function} func The function to cancel.
* Cancels a scheduled debounced function.
* Does nothing if the function is not debounced or not scheduled.
* @param {function} func The function to cancel. Either the original or the debounced function.
*/
export function cancelDebounce(func) {
if (debounceMap.has(func)) {