how to detect a function was called with javascript

后端 未结 5 1998
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 08:18

I have a javascript function.

How to check:

  • if function was called ( in section have this function), then not

5条回答
  •  爱一瞬间的悲伤
    2020-12-05 08:53

    You can use a global variable in a custom namespace to store whether the function has been called.

    if(!window.mynamespace){
        window.mynamespace={};
    }
    
    mynamespace.callMeOnlyOnce=function(){
    
        if(mynamespace.alreadyCalled)return;
    
        alert('calling for the first time');
        mynamespace.alreadyCalled=true;
    };
    
    // alert box comes
    mynamespace.callMeOnlyOnce();
    
    
    // no alert box
    mynamespace.callMeOnlyOnce();
    

提交回复
热议问题