How to enable and disable javascript functions?

前端 未结 2 1063
青春惊慌失措
青春惊慌失措 2021-01-16 03:38
             if(tmpStr == tmp+value)
             {
                i=1;
                action = null;
                action2 = null;
             }
             i         


        
相关标签:
2条回答
  • 2021-01-16 03:54

    Do something like this

    function actionFunc(){
        document.getElementById("movie0").sndToAS("pause");
    }
    function action2Func()
    {
        if(tmp != "movie0"){
         document.getElementById("movie0").sndToAS("pause");
    
        }
    }
    
    window.action = actionFunc;
    window.action2 = action2Func;
    
    if(tmpStr == tmp+value)
    {
        id=1;
        window.action = null;
        window.action2 = null;
    }
    if(tmpStr1 == tmp+value)
    {
        id=0;
        window.action = actionFunc;
        window.action2 = action2Func;
    }
    

    Then when you want to call your functions all you have to do is

    action();
    action2();
    

    but your going to want to check if the are set before calling them

    I believe thats what your after

    or do something like this

    function action(){
        if (window.actionEnabled)
        {
           document.getElementById("movie0").sndToAS("pause");
        }
    }
    function action2()
    {
        if (window.action2Enabled)
        {
           if(tmp != "movie0"){
             document.getElementById("movie0").sndToAS("pause");
           }
        }
    }
    
    window.actionEnabled = true;
    window.action2Enabled = true;
    
    if(tmpStr == tmp+value)
    {
        id=1;
        window.actionEnabled = false;
        window.action2Enabled = false;
    }
    if(tmpStr1 == tmp+value)
    {
        id=0;
        window.actionEnabled = true;
        window.action2Enabled = true;
    }
    

    then you don't have to check if the are enabled just call them and they will work or want depending on if they are enabled

    0 讨论(0)
  • 2021-01-16 04:10

    If you don't want a function to execute when in a particular state, I would wrap your function do a state check and then a return if in an inappropriate state.

    function action(obj){
       if(obj.state == <a state you dont want to execute>)
         return;
    
       action();
    } 
    
    function action(){
       document.getElementById("movie0").sndToAS("pause");
    }
    
    0 讨论(0)
提交回复
热议问题