Skip to content

Commit

Permalink
Merge pull request #238 from siberiacancode/#230
Browse files Browse the repository at this point in the history
#230 🧊 add this for throttle and debounce
  • Loading branch information
debabin committed Sep 4, 2024
2 parents 0b91689 + 39c3b17 commit 091951c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/utils/helpers/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const debounce = <Params extends unknown[]>(
export function debounce<Params extends unknown[]>(
callback: (...args: Params) => void,
delay: number
): ((...args: Params) => void) => {
): ((...args: Params) => void) {
let timer: ReturnType<typeof setTimeout>;

return function (...args: Params) {
return function (this: any, ...args: Params) {
clearTimeout(timer);
timer = setTimeout(() => callback(...args), delay);
timer = setTimeout(() => callback.apply(this, args), delay);
};
};
4 changes: 2 additions & 2 deletions src/utils/helpers/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export const throttle = <Params extends any[]>(
): ((...args: Params) => void) => {
let isCalled = false;

return function (...args) {
return function (this: any, ...args) {
if (!isCalled) {
callback(...args);
callback.apply(this, args);
isCalled = true;
setTimeout(() => {
isCalled = false;
Expand Down

0 comments on commit 091951c

Please sign in to comment.