_.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;
// 如果超过了wait时间,那么就立即执行
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;
};
};
如上所示,在underscore中可以传入leading为false来控制第一次是否立即执行,那么我想问一下if (!previous && options.leading === false) previous = now;这句到底是什么意思?为什么options.leading为false的时候每次都要把now赋值给previous?这样now - previous不是必然为0?这样remaining就是wait了。
但是在options.leading不为false,也就是第一次立即执行的时候,为什么previous却是上次执行的later函数里面记录的时间,这个时候now - previous肯定是大于0的,这样remaining岂不是永远小于wait?
为什么这两种情况下最后的remaining值却不一样?这是怎么处理的?
千巷猫影
慕娘9325324
相关分类