title: how to set and return a variable inside JavaScript function?
body: <p>see the example first then you can understand my question.</p>
  
  <pre><code>var x;
  function checkTime() {
      $.ajax({ 
          type: 'GET', 
          url: 'http://www.example.com/time.php', 
          data: test,
          dataType:'json',
          success: function (data) {
              if (data == 0){
                  x = 'true';
              } else if (data == 1){
                  x = 'false';
              }
          }
      });
  }
  
  checkTime();
  alert (x);
  </code></pre>
  
  <p>the alert will be <code>undefined</code></p>
  
  <p>I need to set <code>x</code> inside the <code>checkTime</code> functions and grab it outside the function</p>
  
  <p>even if i do :</p>
  
  <pre><code>var x = checkTime();
  alert (x);
  </code></pre>
  
  <p>i still get <code>undefined</code></p>
  
  <p>also i only need to check for true or false, maybe there is another way to do things. I also tried:</p>
  
  <pre><code>...
  if (data == 0){
      return true;
  } else if (data == 1){
      return false;
  }
  ...
  </code></pre>
  
  <p>but i don't know how to check for that.</p>
  
  <p>basically i need to so something like :</p>
  
  <p>if (x == 1){  // if(x == true)
      //do something
  }</p>
  
  <p>any ideas?</p>
  
  <p>thanks</p>