如何从我正在使用的 result() 回调函数中捕获返回值? [英] How can I catch the return value from the result() callback function that I'm using?

查看:33
本文介绍了如何从我正在使用的 result() 回调函数中捕获返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
  var geo = new GClientGeocoder();

  function showAddress() {
    var search = document.getElementById("search").value;
    // getLocations has not ret, so wtf!
    geo.getLocations(search, function (result) { (result.Status.code == 200) ? alert(result.Placemark[0].Point.coordinates) : alert(result.Status.code); });
  }</script>

我需要回调的 ret 值,因为 getLocations() 不返回任何值.我该怎么做?

I need the ret value of the callback as getLocations() returns no values. How can I do this?

推荐答案

你不能.您必须以这样一种方式编写代码:需要 result 值的代码在回调中执行.

You can't. You have to write your code in such a way that the code that needs the result value is executed in the callback.

示例(我只是以一种对我来说合乎逻辑的方式命名函数):

function drawPlacemarks(marks) {
   // do fancy stuff with the results
   alert(marks[0].Point.coordinates);
}

function getAddress(callback) {
    var search = document.getElementById("search").value;
    geo.getLocations(search, function (result) { 
        if(result.Status.code == 200) {
           // pass the result to the callback
           callback(result.Placemark);
        }
    });
}

getAddress(drawPlacemarks);

这篇关于如何从我正在使用的 result() 回调函数中捕获返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆