```javascript
防抖: 一定时间内多次操作,只取最后一次触发操作
function debounce(method, delay) {
let timer = null;
return function() {
let args = argument;
let context = this;
if(timer) {
clearTimeOut(timer);
timer = setTimerOut(function() {
method.apply(context, args);
}, delay));
} else {
timer = setTimerOut(function() {
method.apply(context, args);
}, delay));
}
};
}
节流: 一定时间内只能触发一次操作
function throttle(method,delay) {
let timer = null;
let remaining = 0;
previousTime = new Date();
return function() {
let args = argument;
let context = this;
nowTime = new Date();
remaining = nowTime - previousTime;
if(remaining > delay) {
if(timer) {
clearTimeOut(timer);
method.apply(context, args);
previousTime = nowTime;
}
} else {
if(!timer) {
timer = setTimeOut(function() {
method.apply(context, args);
previousTime = new Date();
}, delay - remaining)
}
}
};
}