export const throttle = (func, limit) => {
    let lastFunc;
    let lastRan = null;
    const throttled = function (...args) {
        const context = this;
        if (lastRan === null || Date.now() - lastRan >= limit) {
            func.apply(context, args);
            lastRan = Date.now();
        }
        else {
            if (lastFunc)
                clearTimeout(lastFunc);
            lastFunc = setTimeout(() => {
                func.apply(context, args);
                lastRan = Date.now();
            }, limit - (Date.now() - lastRan));
        }
    };
    // Method to cancel the throttled function
    throttled.cancel = () => {
        if (lastFunc)
            clearTimeout(lastFunc);
        lastFunc = null;
        lastRan = null;
    };
    return throttled;
};
//# sourceMappingURL=throttle.js.map