-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
77 lines (63 loc) · 1.66 KB
/
test.html
File metadata and controls
77 lines (63 loc) · 1.66 KB
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
<!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>
<style>
/* .square {
width: 20%;
height: 0;
padding-top: 20%;
background: orange;
} */
.square {
width: 30%;
overflow: hidden;
background: yellow;
}
.square::after {
content: '';
display: block;
margin-top: 100%;
}
</style>
</head>
<body>
<div class="square"></div>
<button id="throttle">点我节流!</button>
<script>
var n = 100
function foo() {
console.log(n)
return
var n = 100//undefined
}
foo()
</script>
<script>
window.onload = function () {
// 1、获取按钮,绑定点击事件
var myThrottle = document.getElementById("throttle");
myThrottle.addEventListener("click", throttle(sayThrottle, 3000));
}
// 2、节流函数体
const throttle = function (func, delay) {
let flag = true
return function (...args) {
if (!flag) return
flag = false
setTimeout(() => {
func.apply(this, args)
flag = true
}, delay)
}
}
// 3、需要节流的事件
function sayThrottle() {
console.log("节流成功!");
}
</script>
</body>
</html>