title: Can JavaScript function execution be interrupted?
body: <p>Having an object to store data.</p>
  
  <pre><code>var store = {
      elements: [],
      eventsEnabled: true,
      addElement: function(element) {
          this.elements.push(element);
          if (this.eventsEnabled) {
              // Code that triggers event, calls handlers... whatever
          }
      }
  };
  </code></pre>
  
  <p>The act of storing data comes from two events (kind of two producers).  First "producer" does not trigger any event:</p>
  
  <pre><code>setInterval(function() {
      store.eventsEnabled = false;
      store.addElement('hello');
      store.eventsEnabled = true;
  }, 12000);
  </code></pre>
  
  <p>Second does trigger events:</p>
  
  <pre><code>setInterval(function() {
      store.addElement('bye');
  }, 7000);
  </code></pre>
  
  <p>The question is, can the second producer break execution flow of first producer?</p>
  
  <p>I mean, if producer 1 disables events and, before finishing execution (and therefore before events are enabled again), producer 2 starts its execution and adds its element, then no events will be triggered.  Is that possible?  Can that happen?</p>
  
  <p>If so, how can this code be converted to be kind-of thread-safe?</p>