-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (68 loc) · 1.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @description 防抖动函数
* @param {Function} func
* @param {number} wait
* @param {boolean} immediate
* @return {*}
*/
export function debounce(
func: Function,
wait: number,
immediate: boolean
): Function {
let timeout: NodeJS.Timeout | null,
args: any,
context: any | null,
timestamp: number,
result: Function;
const later = function () {
// 据上一次触发时间间隔
const last = +new Date() - timestamp;
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
if (last < wait && last > 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return (...args: any[]) => {
timestamp = +new Date();
const callNow = immediate && !timeout;
// 如果延时不存在,重新设定延时
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = null;
args = [];
}
return result;
};
}
/**
* @description 节流函数
* @param {function} func
* @param {number} delay
*/
export function throttle(func: Function, delay: number) {
let timer: NodeJS.Timeout | null | number = null;
// 闭包记录开始时间
let startTime = Date.now();
return (...args: any[]) => {
const curTime = Date.now();
const remaining = delay - (curTime - startTime);
clearTimeout(timer as NodeJS.Timeout);
// 到时执行一次则重置
if (remaining <= 0) {
func.apply(null, ...args);
startTime = Date.now();
} else {
// 未到时则继续等待
timer = setTimeout(func, remaining);
}
};
}