| 1234567891011121314151617 |
- /**
- * 按调用次数做节流
- * @param {Function} fn 真正要执行的函数
- * @param {number} count 每累计调用 count 次才执行 1 次
- * @returns {Function} 包装后的节流函数
- */
- export function throttleCount(fn, count) {
- if (count <= 0) throw new RangeError('count must > 0');
- let counter = 0;
- return function (...args) {
- counter += 1;
- if (counter >= count) {
- counter = 0;
- return fn.apply(this, args);
- }
- };
- }
|