You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functiononce(fnParam){// your code}letlog=once(function(){console.log(1)})log()// print 1log()// nothing or undefinedlog()// nothing or undefinedlog()// nothing or undefined
The text was updated successfully, but these errors were encountered:
functiononce(fnParam){if(Object.prototype.toString.call(fn)!=="[object Function]"){alert('once函数接受的参数必须是一个函数')return}letflag=truereturnfunction(){if(flag){flag=falsefnParam()}return}}letlog=once(function(){console.log(1)})log()// print 1log()// nothing or undefinedlog()// nothing or undefinedlog()// nothing or undefined
写出一个
once
函数,满足以下条件once
函数只接受一个参数fnParam
,这个参数必须是一个函数once
函数返回一个函数fnResult
fnResult
,则执行一次fnParam
fnResult
,也只会执行一次fnParam
示例如下
The text was updated successfully, but these errors were encountered: