title: Javascript - pause execution until flag becomes true
body: <p>how can i pause a javascript execution until a flag becomes true?</p>
  
  <p>For Example, i've a xml message like this:</p>
  
  <pre><code>[...]
  &lt;action&gt;
      &lt;resource update&gt;id&lt;/resourceupdate&gt;
  &lt;/action&gt;
  &lt;action&gt;
      &lt;event&gt;id1&lt;/event&gt;
  &lt;/action&gt;
  &lt;action&gt;
      &lt;event&gt;id2&lt;/event&gt;
  &lt;/action&gt;
  &lt;action&gt;
      &lt;event&gt;id3&lt;/event&gt;
  &lt;/action&gt;
  [...]
  </code></pre>
  
  <p>I wish that the event nodes are processed only after processing node resourceupdate (which requires more time to be served, as it requires the loading of a page):</p>
  
  <p>in javascript to process this message with an iterator (each) i've tried:</p>
  
  <pre><code>$(_response).find('ACTION').each(function() {       
  if (tagName=="RESOURCEUPDATE") {
      ready = false;
      //load the resource with selected id in an iframe
  } else if (tagName=="EVENT") {
      browserLoaded();   //the waiting function
      eventhandler();  //consume the event
  }
  });
  </code></pre>
  
  <p>the waiting function is:</p>
  
  <pre><code>function browserLoaded() {
  if (!ready) {
      setTimeout(browserLoaded(),1000);
  }
  } 
  </code></pre>
  
  <p>and the ready var becomes true when the iframe is loaded:</p>
  
  <pre><code>$(iframe).load(function() {
      ready = true;
  });
  </code></pre>
  
  <p>but when execute i'll catch this error:</p>
  
  <pre><code> Maximum call stack size exceeded error
  </code></pre>
  
  <p>any ideas?
  thanks!</p>