title: Javascript running code once
body: <p>I only want my JavaScript to run once, <strong>but</strong> I 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: </p>
  
  <pre><code>function never_called_again(args) {
    // do some stuff
    never_called_again = function (new_args) {
     // do nothing
    }
  }
  never_called_again();
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>if (code_happened == undefined) {
      \\ run code
       code_happened = true;
  }
  </code></pre>
  
  <p>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'</p>