title: javascript: wait for result of function that uses setInterval
body: <p>What I want is:</p>
  
  <p>Call a function check_and_wait() that tests whether a condition is true or false. It will keep checking/looping at set intervals for a specified amount of time. </p>
  
  <p>The rest of the program must wait for the result of this test. This is the part that goes wrong. It does not have to use setInterval,that's just how it's implemented now.</p>
  
  <pre><code>function test_condition(){
      // do some test. return true or false
  }
  
  function check_and_wait(){
      var maxtime = 3000;
      var  start = new Date().getTime();
      condition = false;
      var interval = setInterval(function() {
          if(condition){return true;}
          else{
              if(  new Date().getTime() - start &lt; maxtime ){
                 condition = test_condition();
              }
              else{return false;}
          }
      }, 250); 
  
  }
  
  result = check_and_wait();
  if(result){
      //do something
  }
  else{
       //do something else
  }
  </code></pre>