title: Trigger next function on complete of first
body: <p>I have the following javascript geotagging script:</p>
  
  <pre><code>window.onload = function(){
  var x=document.getElementById("output");
  getLocation();
  function getLocation()
  {
  if (navigator.geolocation)
  {
     navigator.geolocation.watchPosition(reverseGeoLookup);
  }
  else
  {
     x.innerHTML="Geolocation is not supported by this browser.";
  }
  }
  
  function reverseGeoLookup(position) {
  console.log(position);
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  var req = new XMLHttpRequest()
   req.open("GET", "http://maps.googleapis.com/maps/api/geocode    /json?latlng="+lat+","+lon+"&amp;sensor=true", true)
  req.onreadystatechange = function() {
    if(req.readyState == 4) {
        var result = JSON.parse(req.response).results
        for(var i = 0, length = result.length; i &lt; length; i++) {
            for(var j = 0; j &lt; result[i].address_components.length; j++) {
                var component = result[i].address_components[j]
                //console.log(component.long_name);
                if(~component.types.indexOf("postal_code")) {
                  var out = document.getElementById('output');
                  out.value = component.long_name;
                  return false;
                }
            }
        }
     }
  }
   req.send()
   }
  
   setTimeout(function(){
   var str = document.getElementById('output').value ;
   var res = str.substring(0,3);
  document.getElementById("demo").innerHTML=res;
   },1000);
  
   setTimeout(function(){
  
   window.location = 'mylocationurl' +      document.getElementById('demo').innerHTML + '/' + 'index.html'
   },7000);
  }
  </code></pre>
  
  <p>The code gets the user location, converts it to UK postcode and then redirects the user to the appropriate page. Currently the website is using <code>setTimeout</code> to tell the browser when to redirect.I would like to change this so as to make the events occur in order ie. find location then on complete shorten to first 3 digits then on complete of that redirect the visitor. Ideally i'd prefer to not use jQuery.
  Can anybody help me? </p>
  
  <p><em>Thanks in advance!</em></p>