Skip to content

Commit d18ff36

Browse files
committed
feat: 增加debounce
1 parent ab3df9f commit d18ff36

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

devui/shared/util/debounce.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
* @param func The function to debounce.
4+
* @param wait The number of milliseconds to delay.
5+
* @param immediate Whether to execute immediately
6+
* @returns Returns the new debounced function.
7+
*/
8+
export function debounce<A extends Array<any>, R = void>(func: (...args: A) => R, wait: number, immediate: boolean): (...args: A) => R {
9+
let timer: number, result: R;
10+
return function (...args: A) {
11+
if (timer) clearTimeout(timer)
12+
if (immediate) {
13+
const localImmediate = !timer
14+
timer = window.setTimeout(() => {
15+
timer = null
16+
}, wait);
17+
if (localImmediate) result = func.apply(this, args)
18+
} else {
19+
timer = window.setTimeout(() => {
20+
func.apply(this, args)
21+
}, wait);
22+
}
23+
return result
24+
}
25+
}

0 commit comments

Comments
 (0)