Javascript 运行一次代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14470516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:53:27  来源:igfitidea点击:

Javascript running code once

javascript

提问by Hamy

I only want my JavaScript to run once, butI cannot control how many times the javascript file is executed. Basically I'm writing a tiny JS snippet into a CMS, and the CMS is actually calling it 5-10 times. So solutions like this:

我只希望我的 JavaScript 运行一次,我无法控制执行 javascript 文件的次数。基本上,我正在将一个很小的 ​​JS 片段写入 CMS,而 CMS 实际上调用了 5-10 次。所以解决方案是这样的:

function never_called_again(args) {
  // do some stuff
  never_called_again = function (new_args) {
   // do nothing
  }
}
never_called_again();

Don't seem to work because as soon as my snippet is run again from the top the function is re-declared, and 'do some stuff' is re-evaluated. Perhaps I'm just not doing it properly, I'm not great with JS. I'm considering using something like try-catch on a global variable, something like

似乎不起作用,因为一旦我的代码段从顶部再次运行,函数就会重新声明,并且“做一些事情”会重新评估。也许我只是做得不好,我不擅长 JS。我正在考虑在全局变量上使用 try-catch 之类的东西,例如

if (code_happened == undefined) {
    \ run code
     code_happened = true;
}

EDIT: There is a consistent state e.g. if I set a variable I can see when my snippet is run again. But having to declare it before I access it, I don't know how to say 'does this variable exist yet'

编辑:有一个一致的状态,例如,如果我设置了一个变量,我可以在我的代码段再次运行时看到。但是在访问它之前必须声明它,我不知道怎么说“这个变量是否存在”

回答by Pointy

Try this:

尝试这个:

var doneTheStuff;
function whatever() {
  if (!doneTheStuff) {
    doneTheStuff = true;
    // do the stuff
  }
}

Redundant variable declarations don't affect the value of the variable. Once one of the functions has set the variable to true, the others won't do anything.

冗余变量声明不会影响变量的值。一旦其中一个函数将变量设置为true,其他函数就不会执行任何操作。

回答by Mike Samuel

if (typeof code_happened === 'undefined') {
  window.code_happened = true;
  // Your code here.
}

The typeofcheck gets you around the fact that the global hasn't been declared. You could also just do if (!window.code_happened)since property access isn't banned for undefined properties.

typeof检查让您绕过尚未声明全局的事实。您也可以这样做,if (!window.code_happened)因为未定义的属性不会禁止属性访问。

回答by GtAntoine

With jQuery, the function .one() may be useful : http://api.jquery.com/one/

使用 jQuery,函数 .one() 可能很有用:http: //api.jquery.com/one/

W3School exemple here : http://www.w3schools.com/jquery/event_one.asp

W3School 示例在这里:http: //www.w3schools.com/jquery/event_one.asp

回答by Joseph Silber

Use a closure, and set a flag. If the flag is true, just return:

使用闭包,并设置一个标志。如果标志是true,只需返回:

if ( ! window.never_called_again  ) {
    window.never_called_again = (function () {
        var ran = false;
        return function (args) {
            if ( ran ) return;
            ran = true;
            // Do stuff
        };
    }());
}

Here's the fiddle: http://jsfiddle.net/U2NCs/

这是小提琴:http: //jsfiddle.net/U2NCs/