title: Javascript Variable Sometimes Undefined
body: <p>I know this question has been asked several times, but I couldn't seem to find a solution that worked for me in any of the previous questions. I have a variable that gets set when my HTML page is done loading, but sometimes when my code tries to access that variable, it says that it is undefined. I'm not sure why, since I believe I am waiting for everything to load properly. This exception seems to happen randomly, as most of the time all the code runs fine. Here's a simplified version of my code:</p>
  
  <pre><code>var globalVar;
  
  function initStuff(filePath) {
      // I wait till the HTML page is fully loaded before doing anything
      $(document).ready(function(){
          var video = document.getElementById("videoElementID");
  
          // My parseFile() function seems to run smoothly
          var arrayOfStuff = parseFile(filePath); 
  
          if (arrayOfStuff == null) {
              console.error("Unable to properly parse the file.");
          } else {
              setGlobalVariable(arrayOfStuff);
              video.addEventListener("play", updateVideoFrame, false);
          }
      });
  }
  
  function setGlobalVariable(arrayOfStuff) {
      window.globalVar = arrayOfStuff;
  }
  
  function updateVideoFrame() {
      // A bunch of other code happens first
  
      // This is the line that fails occasionally, saying 
      // "window.globalVar[0].aProperty.anArray[0] is undefined"
      var test = window.globalVar[0].aProperty.anArray[0].aProperty;
  }
  </code></pre>
  
  <p>The only thing that I can think of that might be causing this problem is some sort of synchronicity issue. I don't see why that would be the case, though. Help please!</p>
  
  <h2>Edit:</h2>
  
  <p>In case the asynchronicity issue is coming from my <code>parseFile(xmlFile)</code> method, here is what I'm doing there. I thought it couldn't possibly be causing the issue, since I force the method to happen synchronously, but in case I'm wrong, here it is:</p>
  
  <pre><code>function parseKML(xmlFile) {
      var arrayOfStuff = new Array();
  
      // Turn the AJAX asynchronicity off for the following GET command
      $.ajaxSetup( { async : false } );
      // Open the XML file
      $.get(xmlFile, {}, function(xml) {
          var doc = $("Document", xml);   
          // Code for parsing the XML file is here
          // arrayOfStuff() gets populated here
      });
  
      // Once I'm done processing the XML file, I turn asynchronicity back on, since that is AJAX's default state
      $.ajaxSetup( { async : true } );
  
      return arrayOfStuff;
  }
  </code></pre>