ThrottleF

JavaScript

function throttleF (func, wait)  {
    let context, args, prevArgs, argsChanged, result;
    let previous = 0;

    return function() {
        let now, remaining;
        if(wait) {
            now = Date.now();
            remaining = wait - (now - previous);
        }

        console.log(remaining);
        context = this;
        args = arguments;
        argsChanged = JSON.stringify(args) != JSON.stringify(prevArgs)
        prevArgs = {...args};
        if(argsChanged || wait && (remaining <= 0 || remaining > wait)) {
            if(wait) {
                previous = now;
            }
            result = func.apply(context, args);
            context = args = null;
        }
        return result;
    };
}

const fun = () => {
	console.log('fun');
}

const t = throttleF(fun, -3000);

t();

setTimeout(() => t(), 750);
setTimeout(() => t(), 1750);
setTimeout(() => t(), 5000);