UNPKG

@cc-heart/utils

Version:

🔧 javascript common tools collection

21 lines (19 loc) • 547 B
/** * 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); } }; } export { useOnce };