title: Using setInterval to test for a condition and when found, clearing interval...any alternatives?
body: <p>What I need is for javascript to keep checking for a condition (similar to a while loop). I am doing this by doing a setInterval and if the condition is met, I clear the interval which then exits or if it has run X amount of times, in the case I have below I have it set to 100.</p>
  
  <p>I don't like this that much, I threw it together because it works. I am checking data from a server and this is sluggish. My main reason why I want an alternative is that I have a loading gif that runs when an AJAX call is started, I'll paste this code at the bottom just to show. It is running sluggish so I believe while the javascript is in the interval, it isn't able to render the gif so the moving image just stops.</p>
  
  <p>Anyone know of an alternative to do what I want?
  I hope this makes sense?</p>
  
  <pre><code>function checkCondition(name,action){
      var timesRun = 0;
      var success = false;
  
          var interval = setInterval(function() {
              timesRun++;
              var container = {};
              container = mapSystem["desktop"];
  
              if(action === "delete"){
                  var isExisting = name in container ;
                  if ( !isExisting || (timesRun === 100)) {
                      //break out of this interval
                      clearInterval(interval);
                      renderCheckList();
                      success = true;
                  } else {
                      // Update mapNodeData with fresh data
                      updateData('desktop', false); // Just updates data
                  }
              }
          }, 2000);
  }
  
  // Code that I have to render a loading GIF. Works great but seems 
  // to stop during the interval
  $('#loading').hide().ajaxStart(function() {
                  $(this).show();
               }).ajaxStop(function() {
                  $(this).hide();
              });
  </code></pre>