@cc-heart/utils
Version:
🔧 javascript common tools collection
23 lines (20 loc) • 569 B
JavaScript
;
/**
* Creates a function that can only be called once.
*
* @param {(...args: any) => any} fn - The function to be called once.
* @returns {(...args: any) => any} - A new function that can only be called once.
*/
function useOnce(fn) {
let __once = false;
if (!(fn instanceof Function)) {
throw new Error('first params must be a function');
}
return function (...args) {
if (!__once) {
__once = true;
return fn.apply(this, args);
}
};
}
exports.useOnce = useOnce;