Skip to content

once(fn)

包装给定的入参函数,返回仅执行一次的函数

入参

参数类型必填说明
fnFunction-

示例

typescript
const arr: number[] = [];
function push(x: number) {
  arr.push(x);
}
// 调用 oncePush 仅会触发一次
const oncePush = once(push);
oncePush(1);
oncePush(2);
oncePush(3);
arr; // [1]