UNPKG

@vuetify/nightly

Version:

Vue Material Component Framework

43 lines 1.12 kB
export function throttle(fn, delay) { let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { leading: true, trailing: true }; let timeoutId = 0; let lastExec = 0; let throttling = false; let start = 0; function clear() { clearTimeout(timeoutId); throttling = false; start = 0; } const wrap = function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } clearTimeout(timeoutId); const now = Date.now(); if (!start) start = now; const elapsed = now - Math.max(start, lastExec); function invoke() { lastExec = Date.now(); timeoutId = setTimeout(clear, delay); fn(...args); } if (!throttling) { throttling = true; if (options.leading) { invoke(); } } else if (elapsed >= delay) { invoke(); } else if (options.trailing) { timeoutId = setTimeout(invoke, delay - elapsed); } }; wrap.clear = clear; wrap.immediate = fn; return wrap; } //# sourceMappingURL=throttle.js.map