Skip to main content

once()

once<T>(fn): (...args) => void

Ensures that the given function is only called once.

Type Parameters

T

T extends SomeFunction

The type of the function to call once.

Parameters

fn

T

The function to call once.

Returns

(...args): void

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