UNPKG

addsearch-js-client

Version:

AddSearch API JavaScript client

39 lines 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const throttle = (delay, callback) => { // Last time the callback was executed let lastExec = 0; // Returned by setTimeout let timeout = null; // Clear existing timeout function clearExistingTimeout() { if (timeout !== null) { clearTimeout(timeout); timeout = null; } } /* * Wrap the callback inside a throttled function */ function wrapper(...args) { const elapsed = Date.now() - lastExec; // Execute callback function const exec = () => { lastExec = Date.now(); callback(...args); }; clearExistingTimeout(); // Execute if (elapsed > delay) { exec(); } // Schedule for a later execution else { timeout = setTimeout(exec, delay - elapsed); } } // Return the wrapper function return wrapper; }; exports.default = throttle; //# sourceMappingURL=throttle.js.map