UNPKG

throttle-typescript

Version:

Helper that returns a throttled function (TypeScript types included - works without TypeScript also)

23 lines (22 loc) 679 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.throttle = void 0; /** * Creates a throttled function that only invokes the provided function (`func`) at most once per within a given number of milliseconds * (`limit`) */ function throttle(func, limit) { var inThrottle; var lastResult; return function () { var args = arguments; var context = this; if (!inThrottle) { inThrottle = true; setTimeout(function () { return (inThrottle = false); }, limit); lastResult = func.apply(context, args); } return lastResult; }; } exports.throttle = throttle;