@parcel/utils
Version:
Blazing fast, zero configuration web application bundler
16 lines (13 loc) • 375 B
JavaScript
// @flow strict-local
export default function throttle<TArgs: Iterable<mixed>>(
fn: (...args: TArgs) => mixed,
delay: number,
): (...args: TArgs) => void {
let lastCalled: ?number;
return function throttled(...args: TArgs) {
if (lastCalled == null || lastCalled + delay <= Date.now()) {
fn.call(this, ...args);
lastCalled = Date.now();
}
};
}