diana
Version:
lightweight tool library
36 lines • 1.02 kB
JavaScript
;
function debounce(func, wait, immediate) {
var timeout;
var debounced = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (timeout)
clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(function () {
timeout = null;
}, wait);
if (callNow)
func.apply(this, args);
}
else {
timeout = setTimeout(function () {
func.apply(_this, args);
timeout = null;
}, wait);
}
};
return debounced;
}
function Debounce(time, immediate) {
return function (target, key, descriptor) {
descriptor.value = debounce(target[key], time, immediate);
};
}
var obj = { debounce: debounce, Debounce: Debounce };
module.exports = obj;
//# sourceMappingURL=debounce.js.map