once()
once<
T
>(fn
): (...args
) =>void
Ensures that the given function is only called once.
Type Parameters
• T extends SomeFunction
Parameters
fn
T
The function to call once.
Returns
Function
Parameters
args
...Parameters
<T
>
Returns
void
Example
let globalVal = 10;
const addGlobal = (n: number) => {
globalVal = globalVal + n;
}
const onceAdd = once(addGlobal);
onceAdd(5); // 15
onceAdd(5); // 15