From c77854667bb98529eb1726c8e5be0797f8cf4b2f Mon Sep 17 00:00:00 2001 From: Sebin Nyshkim Date: Sun, 22 Jan 2023 16:45:14 +0100 Subject: [PATCH] feat: add debounce function --- src/helpers.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/helpers.ts b/src/helpers.ts index f5b35be..4555b16 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,3 +1,17 @@ +const debounce = (fn: Function, delay: number = 300): any => { + let timer = 0; + const debounced = (...args: Array): void => { + if (!args) args = []; + clearTimeout(timer); + + timer = setTimeout(() => { + fn.apply(fn, args); + }, delay); + }; + + return debounced; +}; + const getClientLocale = (): string => { return navigator.languages.length > 0 ? navigator.languages[0] : "en-US"; }; @@ -51,6 +65,7 @@ const dateFormat = new Intl.DateTimeFormat(getClientLocale(), { }); export { + debounce, getClientLocale, getAge, toImperial,