【问题标题】:disable a function from loading when page loads页面加载时禁用加载功能
【发布时间】:2013-12-27 15:55:32
【问题描述】:

我有一些 php、javascript 和 html 代码。我有几行在 2 个函数中是相同的。当页面加载时,我只希望其中一个功能基本上执行而不是另一个,我怎样才能阻止另一个执行?下面的例子:

Function 1()
document.getElementById('field1').length=$field1_numrows

Function 2()
document.getElementById('field1').length=$field1_numrows

我不希望第二个函数运行,现在在第一个函数中出现了一些代码,所以函数 2 实际上是覆盖函数 1 的值。

【问题讨论】:

    标签: javascript php html function load


    【解决方案1】:

    您所需要的只是某种标志。例如:

    var wasExecuted;
    
    function runOnce(){
       if( wasExecuted ){
          return;
       }else{
          wasExecuted = true;
          document.getElementById('field1').length=$field1_numrows;
       }
    }
    
    runOnce(); //this will run through the 'else' 
    runOnce(); //this will exit the runOnce function (as the flag has been set to true)
    

    【讨论】:

    • 谢谢大家,我实际上是在为其他一些 onchange 函数使用一个标志,并且应该也使用它。感谢大家的投入。
    【解决方案2】:

    标志值呢? 喜欢:

    var myFlag = false;
    function 1(){
     if(!myFlag){
      document.getElementById('field1').length=$field1_numrows;
      myFlag = true;
     }
    }
    
    function 2(){
     if(!myFlag){
      document.getElementById('field1').length=$field1_numrows;
      myFlag = true;
     }
    }
    

    【讨论】:

      【解决方案3】:

      您没有在括号中捕获函数是否有原因?试试下面的代码。

      window.onload = function(){
      document.getElementById('field1').length=$field1_numrows
      };
      
      function 2(){
      document.getElementById('field1').length=$field1_numrows
      }
      

      【讨论】:

      • 实际上我正在捕获括号中的代码,忘记发布它(您在此处看到的代码只是一个示例)。有没有另一种方法可以把它放在你可以说不加载或运行函数 2 的地方?我真的不想说onload,虽然我确实尝试过但没有成功。
      • 我也是这么想的,它并没有运行它,而是为变量赋值?
      【解决方案4】:

      也许这里有一个更通用的解决方案:

      var already_executed = (function () {
          var done = {};
          return function (tag) {
              var res = done[tag];
              done[tag] = true;
              return res;
              }
          })();    
      

      标签标识一组功能。只有组中的第一个将被执行。

      您可以通过为不同的功能组分配不同的标签来重用相同的机制。

      例如:

      function a() {
          if (already_executed("a_or_b")) return;
          console.log ("a is executing");
          // rest of the code
      }
      
      function b() {
          if (already_executed("a_or_b")) return;
          console.log ("b is executing");
          // rest of the code
      }
      
      function c() {
          if (already_executed("c_or_d")) return;
          console.log ("c is executing");
          // rest of the code
      }
      
      function d() {
          if (already_executed("c_or_d")) return;
          console.log ("d is executing");
          // rest of the code
      }
      

      打电话

      a();
      b();
      c();
      d();
      

      会导致

      a is executing
      c is executing
      

      说了这么多,我认为重构你的代码比依赖这样的黑客投资更好。在我看来,让错误代码处于这种机制背后是自找麻烦。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多