@web-atoms/core-docs
Version:
38 lines • 1.36 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Defers execution for given milliseconds. And previous pending
* execution is cancelled, so only the last execution will be executed.
*
* This is important when you want to watch multiple events and avoid multiple refresh
* @param n number of milliseconds to defer
*/
function Defer(n = 100) {
return (target, key, descriptor) => {
// tslint:disable-next-line: ban-types
const old = descriptor.value;
descriptor.value = function (...a) {
const k = `_$_timeout_${key}`;
const id = this[k];
if (id) {
clearTimeout(id);
}
this[k] = setTimeout(() => {
this[k] = 0;
old.apply(this, a);
}, n);
};
};
}
exports.default = Defer;
});
//# sourceMappingURL=Defer.js.map