UNPKG

ngx-graph-new

Version:

Modify the ngx-chart version is used

71 lines 1.81 kB
/** * Throttle a function * * @export * @param {*} func * @param {number} wait * @param {*} [options] * @returns */ export function throttle(func, wait, options) { options = options || {}; var context; var args; var result; var timeout = null; var previous = 0; function later() { previous = options.leading === false ? 0 : +new Date(); timeout = null; result = func.apply(context, args); } return function () { var now = +new Date(); if (!previous && options.leading === false) { previous = now; } var remaining = wait - (now - previous); context = this; args = arguments; if (remaining <= 0) { clearTimeout(timeout); timeout = null; previous = now; result = func.apply(context, args); } else if (!timeout && options.trailing !== false) { timeout = setTimeout(later, remaining); } return result; }; } /** * Throttle decorator * * class MyClass { * throttleable(10) * myFn() { ... } * } * * @export * @param {number} duration * @param {*} [options] * @returns */ export function throttleable(duration, options) { return function innerDecorator(target, key, descriptor) { return { configurable: true, enumerable: descriptor.enumerable, get: function getter() { Object.defineProperty(this, key, { configurable: true, enumerable: descriptor.enumerable, value: throttle(descriptor.value, duration, options) }); return this[key]; } }; }; } //# sourceMappingURL=throttle.js.map