-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export declare function debounce(fn: Function, delay: number): Function; | ||
export declare function throttled(fn: Function, delay: number): Function; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Debounce function to prevent multiple calls in a short period of time | ||
* @param {Function} fn - The function to debounce | ||
* @param {number} delay - The delay in milliseconds | ||
* @returns {Function} The debounced function | ||
*/ | ||
export function debounce(fn, delay) { | ||
let timer = 0; | ||
return (...args) => { | ||
window.clearTimeout(timer); | ||
timer = setTimeout(() => fn(...args), delay); | ||
}; | ||
} | ||
|
||
/** | ||
* Throttle function to reduce the trigger rate | ||
* @param {Function} fn - The function to throttle | ||
* @param {number} delay - The delay in milliseconds | ||
* @returns {Function} The throttled function | ||
*/ | ||
export function throttled(fn, delay) { | ||
let timer = 0; | ||
return (...args) => { | ||
const now = (new Date).getTime(); | ||
if (now - timer < delay) { | ||
return; | ||
} | ||
timer = now; | ||
return fn(...args); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters