/* eslint-disable prefer-rest-params */
export function throttle(interval, fn) {
    if (interval <= 0) {
        return fn;
    }
    let timeoutId;
    let lastTrigger = 0;
    return function () {
        // Already scheduled to run.
        if (timeoutId) {
            return;
        }
        const now = Date.now();
        const delay = interval - now + lastTrigger;
        // Waited long enough so execute.
        if (delay <= 0) {
            lastTrigger = now;
            fn.apply(this, [...arguments]);
        }
        else {
            // Schedule for later.
            timeoutId = setTimeout(() => {
                lastTrigger = now;
                timeoutId = undefined;
                fn.apply(this, [...arguments]);
            }, delay);
        }
    };
}
//# sourceMappingURL=throttle.js.map