所以我对 Javascript 比较陌生,并且在检索 JSON 数据时遇到问题,它被调用了两次。具体来说, getData 函数获取 JSON 数据,然后检索 JSON 对象并执行plotMarkers 函数(我正在使用 Google Maps API)。在检查控制台时,回调函数运行两次,这意味着列表项被追加两次。我该如何解决这个问题?
具体代码段:
function initialize() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(-37.811748, 144.962886),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
getData();
}
function getData() {
jQuery.ajax({
url: 'https://dl.dropboxusercontent.com/u/9546194/gigs.json',
dataType: 'json',
success: function(results) {
plotMarkers(results.gigs);
}
});
}
任何帮助将不胜感激!
最佳答案
摆脱onload="initialize()"
在body
.
两者google.maps.event.addDomListener(window, 'load', initialize);
和<body onload="initialize()"
正在调用initialize
.
关于javascript - 回调函数被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23101810/