title: setInterval Returning Value
body: <p>I am working with a setInveral function and I would like to access the value that have been calculated during the interval and return them later. </p>
  
  <p>The function itself, look if an element is finished to load (through Ajax) for a number of time and then return the JSON parse of that element if it is finished (or return blank). 
  It is possible that the element is finished loaded after 200ms or 1s. In order to be the quickest possible, I am actually using setInterval.</p>
  
  <p>My function look like this : </p>
  
  <pre><code>window.ObjectBuilding = function (id){ /*function to check the element &amp; 
  return Object*/
  /*Set max number of loop*/
  var dl_cap = 5;
  /*Set the intervals (in milliseconds) on how often to execute the code*/
  var dl_int = 200;
  /*Counter*/
  var check_dl_inc = 0;
  /*initiate global var*/
  window.temp_object='';
  check = setInterval(function(){
      el = window.document.getElementById(id);
      if(el){
          if(el.innerHTML.length &gt; 50){
              /*Object ready*/
              temp_object= JSON.parse(el.innerHTML);
              clearInterval(check);
          }else{
              /*Oject not yet ready*/
              check_dl_inc++;
              if(check_dl_inc == dl_cap){
                  temp_object= "";
                  clearInterval(check );
                  }
          }
      }else{
          /*Missing Dom element*/
          temp_object= "";
          clearInterval(check );
      }
  }, dl_int);
  return temp_object
  }
  </code></pre>
  
  <p>The idea is to then use this object to a multiple place on my website such as : 
  <code>object = ObjectBuilding('myID')</code></p>
  
  <p>The problem I encounter is that the <code>temp_object</code> is never updated with the latest value. It always remain as the initial state. </p>
  
  <p>I think this is a closure thing but I didn't find a way to get around it. 
  The SetInterval return value is the ID of the temporal object so I cannot get that. </p>