嗨,我正在调用此函数:
function getCoordenadas() { var coordenadas = new Array(); $.post( '<?=$this->baseUrl('user/parse-kml')?>', { kmlName: "esta_chica.kml"}, function(jsonCoord) { jQuery.each(jsonCoord, function(i, val) { var latlng = val.split(','); coordenadas.push(new google.maps.LatLng(latlng[0], latlng[1])); }); }, 'json' ); return coordenadas; }喜欢这个:
$(document).ready(function(){ $('.caller').click(function() { console.log(getCoordenadas()); }); });所以当你点击.caller时它会调用函数获取数据正确填充数组,但是console.log(getCoordenadas()); 输出[]。
如果我从函数范围移动数组声明(var coordenadas = new Array();)以使其成为全局,当我第一次单击.caller时,console.log(getCoordenadas()); 输出[],但第二次正确输出数组。 有任何想法吗?
提前致谢
Hi I'm calling this function:
function getCoordenadas() { var coordenadas = new Array(); $.post( '<?=$this->baseUrl('user/parse-kml')?>', { kmlName: "esta_chica.kml"}, function(jsonCoord) { jQuery.each(jsonCoord, function(i, val) { var latlng = val.split(','); coordenadas.push(new google.maps.LatLng(latlng[0], latlng[1])); }); }, 'json' ); return coordenadas; }like this:
$(document).ready(function(){ $('.caller').click(function() { console.log(getCoordenadas()); }); });So when you click .caller it calls the function gets the data correctly populates the array, but console.log(getCoordenadas()); outputs [].
If I move the array declaration (var coordenadas = new Array();) from the function scope to make it global, when I click .caller for the first time console.log(getCoordenadas()); outputs [], but the second time it outputs the array correctly. Any ideas?
Thanks in advance
最满意答案
此函数异步工作。 AJAX帖子被触发,然后函数返回而不等待AJAX调用完成。 这就是为什么coordenadas数组是空的。
当你把它设为全局时,第一次它仍然是空的,并且第二次你尝试时,ajax返回并填充数组。 您应该重新构建代码以使用回调。 像这样的东西:
// definition function getCoordenadas(callback) { var coordenadas = new Array(); $.post( '<?=$this->baseUrl('user/parse-kml')?>', { kmlName: "esta_chica.kml"}, function(jsonCoord) { jQuery.each(jsonCoord, function(i, val) { var latlng = val.split(','); coordenadas.push(new google.maps.LatLng(latlng[0], latlng[1])); }); callback(coordenadas); }, 'json' ); } // usage $(document).ready(function(){ $('.caller').click(function() { getCoordenadas(function(coord) { console.log(coord); }) }); });This function works asynchronously. AJAX post is fired and then function returns without waiting for AJAX call to complete. That's why coordenadas array is empty.
When you make it global, the first time it's still empty and by the second time you try, the ajax returned and populated the array. You should restructure your code to use a callback. Something like this:
// definition function getCoordenadas(callback) { var coordenadas = new Array(); $.post( '<?=$this->baseUrl('user/parse-kml')?>', { kmlName: "esta_chica.kml"}, function(jsonCoord) { jQuery.each(jsonCoord, function(i, val) { var latlng = val.split(','); coordenadas.push(new google.maps.LatLng(latlng[0], latlng[1])); }); callback(coordenadas); }, 'json' ); } // usage $(document).ready(function(){ $('.caller').click(function() { getCoordenadas(function(coord) { console.log(coord); }) }); });更多推荐
发布评论