|
|
@@ -73,48 +73,35 @@ export function sortTime<T extends object>(arr: T[], key: keyof T, isUp = true)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-const debounceMap = new Map<string, number>();
|
|
|
-const throttleMap = new Map<string, number>();
|
|
|
-
|
|
|
/**
|
|
|
- * 函数防抖 (等待触发)
|
|
|
- * @param callback 到期时间执行的回调
|
|
|
- * @param ms 延迟毫秒数,默认100毫秒
|
|
|
- * @param timeoutId 防抖ID
|
|
|
+ * 函数防抖(等待触发)
|
|
|
+ * @param callback 回调函数
|
|
|
+ * @param delay 延迟毫秒数,默认100毫秒
|
|
|
* @returns
|
|
|
*/
|
|
|
-export function debounce(callback: () => void, ms = 100, timeoutId = 'timer') {
|
|
|
- if (debounceMap.has(timeoutId)) {
|
|
|
- const t = debounceMap.get(timeoutId);
|
|
|
- clearTimeout(t);
|
|
|
+export function debounce<T extends unknown[]>(callback: (...param: T) => void, delay = 100) {
|
|
|
+ let timer = 0;
|
|
|
+ return function (...args: T) {
|
|
|
+ clearTimeout(timer);
|
|
|
+ timer = window.setTimeout(() => {
|
|
|
+ callback(...args);
|
|
|
+ }, delay);
|
|
|
}
|
|
|
-
|
|
|
- const timer = window.setTimeout(() => {
|
|
|
- debounceMap.delete(timeoutId);
|
|
|
- callback();
|
|
|
- }, ms)
|
|
|
-
|
|
|
- debounceMap.set(timeoutId, timer);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 函数节流 (间隔触发)
|
|
|
- * @param callback 到期时间执行的回调
|
|
|
- * @param ms 间隔毫秒数,默认100毫秒
|
|
|
- * @param timeoutId 节流ID
|
|
|
+ * 函数节流(间隔触发)
|
|
|
+ * @param callback 回调函数
|
|
|
+ * @param delay 延迟毫秒数,默认100毫秒
|
|
|
* @returns
|
|
|
*/
|
|
|
-export function throttle(callback: () => void, ms = 100, timeoutId = 'timer') {
|
|
|
- if (throttleMap.has(timeoutId)) {
|
|
|
- const t = throttleMap.get(timeoutId);
|
|
|
- if (t) return;
|
|
|
+export function throttle<T extends unknown[]>(callback: (...param: T) => void, delay = 100) {
|
|
|
+ let timer = 0;
|
|
|
+ return function (...args: T) {
|
|
|
+ if (timer) return;
|
|
|
+ timer = window.setTimeout(() => {
|
|
|
+ timer = 0;
|
|
|
+ callback(...args);
|
|
|
+ }, delay);
|
|
|
}
|
|
|
-
|
|
|
- let timer = window.setTimeout(() => {
|
|
|
- timer = 0;
|
|
|
- throttleMap.delete(timeoutId);
|
|
|
- callback();
|
|
|
- }, ms);
|
|
|
-
|
|
|
- throttleMap.set(timeoutId, timer);
|
|
|
}
|