简体   繁体   English

Javascript运行代码一次

[英]Javascript running code once

I only want my JavaScript to run once, but I cannot control how many times the javascript file is executed.我只希望我的 JavaScript 运行一次,我无法控制 JavaScript 文件执行的次数。 Basically I'm writing a tiny JS snippet into a CMS, and the CMS is actually calling it 5-10 times.基本上我正在将一个小的 JS 片段写入 CMS,而 CMS 实际上会调用它 5-10 次。 So solutions like this:所以像这样的解决方案:

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.也许我只是做得不好,我对 JS 不是很好。 I'm considering using something like try-catch on a global variable, something like我正在考虑在全局变量上使用类似 try-catch 的东西,比如

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

EDIT: There is a consistent state eg 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'但是在我访问它之前必须声明它,我不知道怎么说'这个变量是否存在'

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 ,其他函数将不会做任何事情。

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

The typeof check gets you around the fact that the global hasn't been declared. typeof检查让您了解全局尚未声明的事实。 You could also just do if (!window.code_happened) since property access isn't banned for undefined properties.你也可以只做if (!window.code_happened)因为未定义的属性不会禁止属性访问。

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/

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

In this way, the code is executed only once.这样,代码只执行一次。

if(typeof onceRun == "undefined") window.onceRun=(
    ()=>{
        //your codes...
        console.log("runing...")
    return true
    }).call()

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

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