简体   繁体   English

在页面加载时禁用函数加载

[英]disable a function from loading when page loads

I have some php, javascript and html code. 我有一些php,javascript和html代码。 I have a few lines that are identical in 2 functions. 我在2个函数中有几行相同。 When the page loads I just want one of those functions to basically execute and not the other, how can I prevent the other from executing? 当页面加载时,我只希望其中一个函数基本执行,而不希望另一个函数执行,如何防止另一个函数执行? Example below: 下面的例子:

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

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

I don't want the 2nd function to run, right now in the first function there is some code that occurs, so function 2 is actually overwriting function 1's value. 我不希望第二个函数运行,现在在第一个函数中发生了一些代码,因此函数2实际上覆盖了函数1的值。

All you need is some sort of a flag. 您所需要的只是某种标志。 For example: 例如:

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)

Is there a reason why you aren't capturing your functions in brackets? 为什么没有在括号中捕获函数? Try the below code. 试试下面的代码。

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

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

What about a flag value? 那么标志值呢? like: 喜欢:

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;
 }
}

Perhaps a more generic solution here : 也许这里是一个更通用的解决方案:

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

A tag identifies a group of functions. 标签标识一组功能。 Only the first of the group will be executed. 仅执行组中的第一个。

You can reuse the same mechanism with different groups of functions by assigning them different tags. 您可以通过为不同的功能组分配不同的标记来重复使用相同的机制。

For instance : 例如 :

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
}

Calling 调用

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

Will result in 将导致

a is executing
c is executing

All this being said, I think refactoring your code would be a better investment than relying on such hacks. 综上所述,我认为重构代码比依赖于此类黑客更好。 Keeping wrong code dormant behind such a mechanism is asking for trouble, in my opinion. 在我看来,使错误的代码处于这种机制的后面一直在麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM