-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.html
68 lines (59 loc) · 1.44 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>debounce的实现学习</title>
<style type="text/css">
body {
height: 20000px;
background: #f2f2f2;
}
</style>
</head>
<body>
<input type="text" onInput="test()" value="debounce" />
<script type="text/javascript">
// 弹跳球
const debounce = function(fn, delay) {
return (function() {
const context = this
const args = arguments
clearTimeout(window.timer)
window.timer = setTimeout(function() {
console.log('debounce')
fn.apply(context, args)
}, delay)
})()
}
// 节流阀
const throttle = function(fn, threshhold) {
var last
var timer
threshhold || (threshhold = 250)
return function () {
var context = this
var args = arguments
var now = Date.now()
if(last && (now < (last + threshhold))) {
clearTimeout(timer)
timer = setTimeout(function() {
last = now
fn.apply(context, args)
}, threshhold)
} else {
last = now
fn.apply(context, args)
}
}
}
const test = function () {
debounce(function() {
console.log('2s后才能出现都考debounce函数')
}, 500)
}
window.addEventListener('scroll', throttle(function() {
console.log('resize')
}, 2000))
</script>
</body>
</html>