title | description | author | tags |
---|---|---|---|
Once Function |
Ensures a function is only called once. |
axorax |
function,once |
const once = (func) => {
let called = false;
return (...args) => {
if (!called) {
called = true;
return func(...args);
}
};
};
// Usage:
const initialize = once(() => console.log('Initialized!'));
initialize(); // Output: Initialized!
initialize(); // No output