@cc-heart/utils
Version:
🔧 javascript common tools collection
18 lines • 515 B
text/typescript
/**
* 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.
*/
export function useOnce(fn: (...args: any) => any) {
let __once = false
if (!(fn instanceof Function)) {
throw new Error('first params must be a function')
}
return function (this: any, ...args: any) {
if (!__once) {
__once = true
return fn.apply(this, args)
}
}
}