主题
_.before(n, func)
_.before(n, func) 创建一个函数,调用不超过指定次数时才会执行另一个函数。
n:指定的调用次数。func:在调用次数未达到n时要执行的函数。
示例:
javascript
// 创建一个函数,调用不超过 3 次时执行指定函数
const beforeThreeCalls = _.before(3, () => {
console.log("Function executed before 3 calls.");
});
// 调用函数
beforeThreeCalls();
beforeThreeCalls();
beforeThreeCalls();
// 仅输出:Function executed before 3 calls.在这个例子中,beforeThreeCalls 函数在被调用了不超过 3 次时执行了指定的函数。