开发者

How to access a global variable in a closure? (Javascript)

开发者 https://www.devze.com 2023-03-26 03:16 出处:网络
var events = <?php echo $sf_data->getRaw(\'jsonData\'); ?>;//some perfecly fine json var l开发者_开发百科ocations = new Array();
            var events = <?php echo $sf_data->getRaw('jsonData'); ?>;//some perfecly fine json
            var l开发者_开发百科ocations = new Array();
            var geocoder = new google.maps.Geocoder();
            $(events).each(function(i,e){
                geocoder.geocode({address: e.place},function(results,statusCode){
                    locations[e.id] = results[0].geometry.location;
                })
            })
            alert(locations.length);

Okay, the question is easy. How do i modify the locations array inside of closure? The alert shows 0.


The alert shows 0 because you modify locations asynchronously. Anonymous function where you modify locations will be called later then you show alert.

0

精彩评论

暂无评论...
验证码 换一张
取 消