【问题标题】:Accessing Returned Javascript Variable in another Script在另一个脚本中访问返回的 Javascript 变量
【发布时间】:2011-09-27 16:48:24
【问题描述】:

我有脚本 (a),它是一个 javascript 脚本,我在其中执行了这个函数:

function csf_viewport_bounds() {
    var bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var maxLat = ne.lat();
    var maxLong = ne.lng();
    var minLat = sw.lat();
    var minLong = sw.lng();

    var the_maps_bounds = [maxLat, maxLong, minLat, minLong];

    return(the_maps_bounds);
 }

我还有 jQuery 脚本 (b)。它有一行调用(a)中的函数:

google.maps.event.addListener(map, 'tilesloaded',  csf_viewport_bounds);

如何访问脚本 (b) 中 (a) 返回的内容 the_maps_bounds?

谢谢。

【问题讨论】:

    标签: javascript jquery google-maps scope


    【解决方案1】:

    我假设你想在 tilesloaded 之后做一些涉及 the_maps_bounds 的事情,在这种情况下你会做类似的事情:

    google.maps.event.addListener(map, 'tilesloaded',  function() {
        // This code will only execute after event `tilesloaded` happens
    
        var the_maps_bounds = csf_viewport_bounds()
    
        // now do something with the bounds
        // ...
    });
    

    【讨论】:

      【解决方案2】:

      只需再次调用该函数,因为它不使用任何参数,并将结果分配给一个变量。

      var myStuff = csf_viewport_bounds()
      

      【讨论】:

        【解决方案3】:

        将脚本 (b) 更改为:

        var bounds = csf_viewport_bounds();
        google.maps.event.addListener(map, 'tilesloaded', bounds);
        // csf_viewport_bounds() return saved as local variable bounds.
        

        【讨论】:

          猜你喜欢
          • 2013-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-16
          • 1970-01-01
          • 2018-01-19
          • 2022-11-26
          • 2022-07-02
          相关资源
          最近更新 更多