// 节流函数
function throttle2(method, duration) {
// 当前时间间隔内是否有方法执行,设置一个开关标识
var runFlag = false;
// 返回一个事件处理函数
return function(e) {
// 判断当前是否有方法执行,有则什么都不做,若为true,则跳出
if (runFlag) {
return false;
}
// 开始执行
runFlag = true;
// 添加定时器,在到达时间间隔时重置锁的状态
setTimeout(function() {
method(e);
// 执行完毕后,声明当前没有正在执行的方法,方便下一个时间调用
runFlag = false;
}, duration);
};
}
// 防抖函数
function debounce(method, duration) {
var timer = null;
return function() {
var that = this,
args = arguments;
// 在本次调用之间的一个间隔时间内若有方法在执行,则终止该方法的执行
if (timer) {
clearTimeout(timer);
}
// 开始执行本次调用
timer = setTimeout(function() {
method.apply(that, args);
}, duration);
};
}