简体   繁体   English

从回调中获取值 function

[英]getting a value from a callback function

I'm trying to return a value from a callback function and assign it to a variable, though am struggling to work it out - any help would be really appreciated....我正在尝试从回调 function 返回一个值并将其分配给一个变量,尽管我正在努力解决它 - 任何帮助都会非常感激......

var latlng1;

function getLocation(){
  navigator.geolocation.getCurrentPosition (function (position){
    coords = position.coords.latitude + "," + position.coords.longitude;
    callback();         
  })
}

//how can I assign the coords value from the callback to variable latlng1 with global scope?
getLocation (function(){
  //alert(coords);
  return coords;
})

// -----------
//I'm trying something like this....but no joy
latlng1 = getLocation (function(){
  return coords;
}

I'm confused as whether or not you want the callback to have access to the coords value or to simply return it from the getLocation function. If it's just to have coords available to the callback then pass it as a parameter.我很困惑,您是否希望回调能够访问coords值,或者只是从getLocation function 返回它。如果只是让coords可用于回调,则将其作为参数传递。

function getLocation(callback) {
  navigator.geolocation.getCurrentPosition (function (position){
    var coords = position.coords.latitude + "," + position.coords.longitude;
    callback(coords);         
  })
}

getLocation (function(coords){
  alert(coords);
})

On the other hand if it's to have it assigned to the return of getLocation then that's not possible.另一方面,如果要将它分配给getLocation的返回值,那是不可能的。 The getCurrentPosition API is asynchronous and hence you can't synchronously return it from the getLocation method. getCurrentPosition API 是异步的,因此您不能从getLocation方法同步返回它。 Instead you need to pass the call back which wants to use coords .相反,您需要传回想要使用coords的回调。

EDIT编辑

OP said they just want the coords value in latlng1 . OP 说他们只想要latlng1中的coords值。 Here is how you accomplish that这是你如何做到这一点

var latlng1;
function getLocation() {
  navigator.geolocation.getCurrentPosition (function (position){
    var coords = position.coords.latitude + "," + position.coords.longitude;
    latlng1 = coords; 
  })
}

Note though that this doesn't change the asynchronous nature of the API. The variable latlng1 won't have the coords value until the async call completes.请注意,尽管这不会更改 API 的异步性质。在异步调用完成之前,变量latlng1不会具有coords值。 Because this version doesn't use a callback you have no way of knowing when that completes (other than checking latlng1 for undefined因为这个版本不使用回调你无法知道什么时候完成(除了检查latlng1 undefined

How about:怎么样:

var latlng1;

function getLocation(){
  navigator.geolocation.getCurrentPosition (function (position){
    latlng1 = position.coords.latitude + "," + position.coords.longitude;
    callback();         
  })
}

getLocation (function(){
  alert(latlng1);
})

You can pass coords to the callback call, and define a parameter for it in the callback.您可以将坐标传递给回调调用,并在回调中为其定义一个参数。 It's easier to read than to try to explain:阅读比尝试解释更容易:

var latlng1;

function getLocation(callback){
  navigator.geolocation.getCurrentPosition (function (position){
    coords = position.coords.latitude + "," + position.coords.longitude;
    callback(coords);         
  })
}

//how can I assign the coords value from the callback to variable latlng1 with global scope?
getLocation (function(coords){
  //alert(coords);
  return coords;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM