title: How to have a function wait until an object's value is not undefined js setTimeout
body: <p>I need to stop a function from proceeding until my php script gets the contents from a file that takes a bit of time to load. Once loaded I update the object that holds this information on my JavaScript file: <code>var setData = seoApp.siteData.result.wordCount;</code></p>
  
  <p>I have created a function that updates my html elements based on the results of the <code>wordCount</code>. </p>
  
  <p>I want my script to continually check if the var <code>setData</code> is not undefined every few seconds to wait for the script to load the setData. I thought I could do this with setTimeout using this code: </p>
  
  <pre><code>for (var i = 0; i &lt; 10; i++) {
      setTimeout(function () {
        console.log(i);
        if(setData !== undefined){
          // stops the loop from running again
          i =  11
          //run if statements here.
        }else {
  
        }
      }, 6000);
    }
  </code></pre>
  
  <p>Well that didn't work. It waits a few seconds and then fires the for loop without waiting for six seconds until the next one.</p>
  
  <p>What am I doing wrong and is this the best way to approach this?</p>
  
  <p>As requested the php script to scrape the data:</p>
  
  <pre><code>&lt;?php
  
  $url = $_GET["url"];
  $string = $_GET["keywords"];
  
  
    libxml_use_internal_errors(true); //Prevents Warnings, remove if desired
    $content = file_get_contents($url);
    $explodedContent = explode("&lt;title&gt;", $content);
    $explodedExplodedContent = explode("&lt;/title&gt;", $explodedContent[1]);
  
    $explodedBody = explode("&lt;body&gt;", $content);
    $explodedExplodedBody = explode("&lt;/body&gt;", $explodedBody[0]);
  
    echo "{ \"result\": ". "{ ";
    echo "\"titleCount\": " . substr_count($explodedExplodedContent[0], $string) . ", "; // title of that page.
    echo "\"bodyCount\": " . substr_count(strip_tags($explodedExplodedBody[0]), $string);
    echo " } }";
  ?&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>