-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththrottle.html
48 lines (45 loc) · 1.58 KB
/
throttle.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
<!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>节流</title>
</head>
<body>
<p>throttle节流:无限滚动,在滚动期间每隔一定的时间段去响应判断滚动条是否已经到达底部。</p>
<p>使用场景:</p>
<p>通过设置定时器,让高频连续触发的事件每隔一定的时间长度之后再做出响应,以规律的时间间隔去执行</p>
<div id="common"></div>
<div id="special"></div>
</body>
<script>
let common = document.getElementById('common'); // 获取页面的左边
let special = document.getElementById('special'); // 获取页面的右边
function throttle (fn, delay,mustRunDelay,context) {
let startTime, timestamp, timer;
return function () {
timestamp = +new Date(); // 设置开始的时间
clearTimeout(timer);
if(!startTime) {
startTime = timestamp
}
if (timestamp - startTime >= mustRunDelay) {
fn.apply(context)
startTime = timestamp
} else {
timer = setTimeout(function (){
fn.apply(context)
},delay)
}
}
}
let middle = throttle(commonWay, 1000, 500)
window.addEventListener('resize', function (){
middle()
})
function commonWay () {
common.innerHTML += '<li>k</li>'
}
</script>
</html>