-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.html
158 lines (131 loc) · 4.88 KB
/
debounce.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="test">test</button>
<script>
function throttle(handler, delay) {
if (typeof handler !== 'function') throw new Error('parameter "hanlder" type must be a Function.');
if (typeof delay === "undefined") delay = 300;
let lock = false;
return function proxy(...args) {
if (lock) return
lock = true
const self = this || window
setTimeout(function () {
lock = false
}, delay)
return handler.apply(self, args)
}
}
function throttle2(handler, delay) {
if (typeof handler !== 'function') throw new Error('parameter "hanlder" type must be a Function.');
if (typeof delay === "undefined") delay = 300;
let timer = null
let previous = 0;
return function proxy(...args) {
let now = new Date()
const self = this || window
const interval = delay - (now - previous)
if (interval <= 0) {
clearTimeout(timer)
timer = null
const ret = handler.apply(self, args)
previous = new Date()
return ret
} else if (!timer) {
timer = setTimeout(function () {
clearTimeout(timer)
timer = null
const ret = handler.apply(self, args)
previous = new Date()
return ret
}, interval)
}
}
}
function debounce(handler, delay, immediate) {
if (typeof handler !== 'function') throw new Error('parameter "hanlder" type must be a Function.');
if (typeof delay === "undefined") delay = 300;
if (typeof delay === "boolean") {
immediate = delay;
delay = 300;
}
let timer;
return function proxy(...args) {
if (timer) clearTimeout(timer);
let self = this || window;
let init = immediate && !timer
timer = setTimeout(function () {
timer = null
const ret = !immediate ? handler.apply(self, args) : null;
return ret;
}, delay)
if (init) {
return handler.apply(self, args);
}
}
}
const testFunc = debounce(function (e) {
console.log('fired')
}, 3000,true)
const button = document.getElementById('test')
button.addEventListener('click', testFunc)
// window.addEventListener('mousemove', testFunc)
function throttle_1(cb, delay) {
if (typeof cb !== "function") {
throw new Error('property callback type should be a Function.')
}
if (!delay) delay = 1000
let timer = null
let previous = 0
return function (...args) {
const curr = new Date();
const interval = delay - (curr - previous)
const self = this || window
if (interval <= 0) {
clearTimeout(timer)
timer = null
cb.apply(self, args)
previous = new Date();
} else if (!timer) {
timer = setTimeout(() => {
clearTimeout(timer)
timer = null
cb.apply(self, args)
previous = new Date();
}, interval)
}
}
}
function debounce_1(cb, delay, immediate) {
if (typeof cb !== "function") {
throw new Error('property callback type should be a Function.')
}
if (typeof delay === "undefined") {
delay = 500
}
if (typeof delay === "boolean") {
immediate = delay
delay = 500
}
let timer = null
return function (...args) {
if (timer) clearTimeout(timer)
const self = this || window
const init = immediate && !timer
timer = setTimeout(() => {
timer = null
!immediate ? cb.apply(self, args) : null
}, delay)
if (init) cb.apply(self, args)
}
}
</script>
</body>
</html>