开发者

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

            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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜