【问题标题】:How can I catch the return value from the result() callback function that I'm using?如何从我正在使用的 result() 回调函数中捕获返回值?
【发布时间】:2011-03-15 18:13:21
【问题描述】:
<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() 不返回任何值。我该怎么做?

【问题讨论】:

    标签: javascript callback anonymous-function


    【解决方案1】:

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

    示例(我只是以我认为合乎逻辑的方式命名了函数):

    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);
    

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多