-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuckYou2.html
65 lines (58 loc) · 1.75 KB
/
fuckYou2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
// const debounce = (func, wait, ...args) => {
// let timeout;
// return function(){
// const context = this;
// if (timeout) clearTimeout(timeoout);
// timeout = setTimeout(() => {
// func.apply(context, ...args)
// },wait);
// }
// }
// let flag = 0; // 记录当前函数调用次数
// // 当用户滚动时被调用的函数
// function foo() {
// flag++;
// console.log('Number of calls: %d', flag);
// }
// // 在 debounce 中包装我们的函数,过 2 秒触发一次
// document.body.addEventListener('scroll', debounce(foo, 2000));
// 综合版????
/**
* @desc 函数防抖
* @param func 函数
* @param wait 延迟执行毫秒数
* @param immediate true 表立即执行,false 表非立即执行
*/
function debounce(func,wait,immediate) {
var timeout;
return function () {
var context = this;
var args = arguments;
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(function(){
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
}
else {
timeout = setTimeout(function(){
func.apply(context, args)
}, wait);
}
}
}
</script>
</html>