scrivito
Version:
Scrivito is a professional, yet easy to use SaaS Enterprise Content Management Service, built for digital agencies and medium to large businesses. It is completely maintenance-free, cost-effective, and has unprecedented performance and security.
35 lines (28 loc) • 830 B
text/typescript
import { setTimeout } from 'scrivito_sdk/common';
import { type TimeoutType } from 'scrivito_sdk/common/timeout';
let throttleDisabled = false;
export function throttle<T extends unknown[]>(
fn: (...args: T) => void,
ms: number
): (...args: T) => void {
if (throttleDisabled) return fn;
let lastTime = 0;
let timeoutId: TimeoutType | undefined;
let lastArgs: T;
function execute() {
clearTimeout(timeoutId);
timeoutId = undefined;
lastTime = Date.now();
fn(...lastArgs);
}
return function (...args) {
lastArgs = args;
const remainingMs = lastTime + ms - Date.now();
if (remainingMs <= 0) execute();
else timeoutId ||= setTimeout(execute, remainingMs);
};
}
// For test purpose only
export function enableThrottle(enabled: boolean): void {
throttleDisabled = !enabled;
}