首先触发下一个功能

时间:2014-01-02 11:43:17

标签: javascript function triggers callback

我有以下javascript地理标记脚本:

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+"&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 < length; i++) {
          for(var j = 0; j < 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);
}

代码获取用户位置,将其转​​换为英国邮政编码,然后将用户重定向到相应的页面。目前网站正在使用setTimeout告诉浏览器何时重定向。我想改变这一点,以使事件按顺序发生,即。找到位置,然后完成缩短到前3位数,然后完成重定向访客。理想情况下,我宁愿不使用jQuery。 有谁能够帮我?

提前致谢!

2 个答案:

答案 0 :(得分:0)

你可以通过回调功能获得成功。

    function getLocation(param, callback) {
      getLocation stuff
      callback();
    } 

答案 1 :(得分:0)

对不起......只是现在我想了解你需要什么...

所以......你需要一个在“getLocation”功能结束时触发的回调函数吗?

如果在完全加载XMLHttpRequest时调用回调函数,则需要在XMLHttpRequest对象的onreadystatechange属性中插入回调函数。 一个例子:

function getLocation(){
    if (navigator.geolocation){
       navigator.geolocation.watchPosition(reverseGeoLookup(position,function(longName){
           console.log("function called, scope:",this,"args:",arguments);
           //callback function here
           //you can insert inside the callback the script inside "setTimeout"
           var res = longName.substring(0,3);
           document.getElementById("demo").innerHTML=res;

           //and after 7 sec. from the ajax request complete change page:
           setTimeout(function(){
                  window.location = 'mylocationurl' +      document.getElementById('demo').innerHTML + '/' + 'index.html'
           },7000);
       }));
    }else{
       x.innerHTML="Geolocation is not supported by this browser.";
}

window.onload = function(){
    var x=document.getElementById("output");
    getLocation();
}

function reverseGeoLookup(position,callback) {
    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+"&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 < length; i++) {
              for(var j = 0; j < 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;

                    //insert here the callback if you need to wait fully request load
                    //note the argument inside of callback become longName.
                    callback(component.long_name);

                   return false;
                  }
              }
          }
       }

     }
     req.send();
 }
}

我希望它可以提供帮助