var helpers = {
/**
* 防抖
* debouncing, executes the function if there was no new event in $wait milliseconds
* @param func
* @param wait
* @param scope
* @returns {Function}
*/
debounce: function (func, wait, scope) {
var timeout;
return function () {
var context = scope || this, args = arguments;
var later = function () {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
},
/**
* 节流
* in case of a "storm of events", this executes once every $threshold
* @param fn
* @param threshhold
* @param scope
* @returns {Function}
*/
throttle: function (fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
}
节流和防抖
最新推荐文章于 2024-08-21 15:43:48 发布