-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathindex.html
More file actions
87 lines (79 loc) · 1.78 KB
/
index.html
File metadata and controls
87 lines (79 loc) · 1.78 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
78
79
80
81
82
83
84
85
86
87
<!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>
<style>
body{
height:2000px;
background:red;
}
</style>
</head>
<body>
<div>
</div>
<script>
const throttle = (fn, wait=50)=>{
let lastTime = 0
return function(...args){
// 返回节流的函数
let now = new Date()
if(now-lastTime>wait){
// 喝上次执行的时间差,大于指定的间隔
lastTime = now
fn.apply(this, args)
}
}
}
const debounce = (fn, wait=500)=>{
let timer
return function(...args){
if(timer) clearTimeout(timer)
timer = setTimeout(()=>{
fn.apply(this, args)
}, wait)
}
}
// const obj = {
// name:'开课吧',
// sayHi: debounce(function(){
// console.log(this)
// })
// }
// obj.sayHi()
// const sayHi = debounce(function(){
// console.log(this)
// })
// sayHi()
// setInterval(
// throttle(()=>{
// console.log('节流后')
// },1000),
// 50
// )
let obj1 = {
name:'开课吧'
}
let obj2 = {
name:'前端性能优化'
}
function sayHi(wor1, word2){
console.log(this.name, wor1, word2)
}
sayHi(1,2)
sayHi.apply(obj1, [1,2])
sayHi.call(obj2, 1, 2)
// window.addEventListener('scroll', throttle(function(){
// console.log('滚动监听, 图片懒加载')
// }, 2000)
// )
window.addEventListener('scroll', debounce(()=>{
console.log('滚动监听, 图片懒加载')
}, 2000)
)
</script>
</body>
</html>