/**
* Returns a function that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds.
*
* @param {Function} fn
* @param {Number} wait In milliseconds.
* @return {Function}
*/
Showcase.debounce = function (fn, wait) {
var timeout;
return function () {
clearTimeout(timeout);
var that = this, args = arguments;
timeout = setTimeout(function () {
fn.apply(that, args);
}, wait);
};
};
/**
* Returns a function that will fire every N milliseconds at most.
*
* @param {Function} fn
* @param {Number} threshold
*/
Showcase.throttle = function (fn, threshhold) {
var last, deferTimer;
return function () {
var that = this, args = arguments, now = new Date().getTime();
if (last && now < last + threshhold) {
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(that, args);
}, threshhold);
} else {
last = now;
fn.apply(that, args);
}
};
};
Comments
0 B
|👍
/👎