浅谈函数节流与函数去抖
函数节流与函数去抖在很多场景都有着应用,对web性能有很大提升。但是这二者也有一些区别,在这里我简单的总结一些基本知识。
函数节流:throttle
函数节流的目的
有些函数不是由用户直接控制触发的,函数有可能被频繁地调用而导致性能问题。一些常见的函数被频繁调用场景:
- window.onscroll事件。当绑定了scroll事件,只要浏览器的窗口滚动就会触发函数,触发频率非常高。如果绑定的函数里面还有DOM操作,则性能损耗非常大。
- mousemove事件。给div绑定该事件,当鼠标拖曳节点时触发函数。
- 键盘keydown监听
我们的目的就是减少函数触发的频率
函数节流原理
我们想到用setTimeout延迟时间执行函数,这样就可以避免函数在一段时间内执行多次。
1 | var throttle = function ( fn, delay ) { |
函数去抖:debounce
函数去抖的目的
函数去抖其实和节流很像,但不同的是去抖是让函数在一段时间内只触发执行一次,而节流是改变函数触发的调用周期。应用场景:
文字输入监听
onscroll事件
onresize事件。
函数去抖原理
也是利用setTimeout和闭包的原理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18var debounce = function ( fn, delay ) {
var context,
args,
timer;
return function () {
context = this, args = [].slice.call(arguments);
if( timer ) {
clearTimeout( timer );
}
timer = setTimeout( function () {
fn.apply( context, args );
}, delay );
};
};
window.onresize = debounce( function () {
console.log("has changed");
}, 600 );underscore源码
underscore里对throttle和debounce有更完整的实现,这里把代码贴出来
throttle
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_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};debounce
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_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp; // 上一次函数触发的时间
if (last < wait && last >= 0) { // 如果没有到达规定的wait时间则继续延迟,相当于计时器
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) { // 如果是立即调用或者上一次函数已经调用完毕
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
本文标题:浅谈函数节流与函数去抖
文章作者:flyrk
发布时间:2017-05-01
最后更新:2022-02-25
原始链接:https://flyrk.github.io/2017/05/01/throttle-debounce/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!
分享