the-platform
Version:
React Hooks and Suspense-ready Components for Web API's and elements
29 lines (26 loc) • 716 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.throttle = throttle;
function throttle(func) {
var threshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 250;
var scope = arguments.length > 2 ? arguments[2] : undefined;
var last, deferTimer;
return function () {
var context = scope || this;
var now = Date.now(),
args = arguments;
if (last && now < last + threshold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
func.apply(context, args);
}, threshold);
} else {
last = now;
func.apply(context, args);
}
};
}