防抖动与节流

一、防抖动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 防抖动
* @method debounce
* @param {function} callback 回调函数
* @param {number} delay 时间间隔
* */
export const debounce = function(callback, delay) {
let timer = null;
return function() {
let context = this;
let args = arguments;

clearTimeout(timer);

timer = setTimeout(function() {
callback.apply(context, args);
}, delay);
}
};


二、节流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 节流
* @method throttle
* @param {function} callback 回调函数
* @param {number} delay 时间间隔
* */
export const throttle = function(callback, delay) {
let prev = Date.now();
return function() {
let context = this;
let args = arguments;
let now = Date.now();
if (now - prev >= delay) {
callback.apply(context, args);
prev = Date.now();
}
}
};