首页 > 解决方案 > 在整个回调函数中保留变量值

问题描述

我有这个功能:

var contor = 0;
function processImobs(){
  for( var i = 0; i < availableImobs.length; ++i){
        processEachImob(availableImobs[i]); 
    }
}

在我的 processEachImob 函数中,我从谷歌地图调用了附近的搜索方法:

       service = new google.maps.places.PlacesService(map);
        service.nearbySearch({ 
            location:  new google.maps.LatLng(imob[0],  imob[1]),
            radius: 500,
            type: ['pharmacy']
            },function callBack(results, status){   
            //cate tipuri de places primesc 
                markersP= []; 
                if (status == google.maps.places.PlacesServiceStatus.OK){
                    for (var i = 0; i < results.length; i++) {
                        createPharmaciesMarker(results[i]);
                    }   
                    //console.log(markersP.length + " " +imob[0]);
                    if( markersP.length !=0){
                        for(var i=0; i < markersP.length; ++i){
                            ++contor;
                            var latLng = [markersP[i].position.lat(), markersP[i].position.lng()];
                            queue.add(imob, latLng, 'pharmacy');        

                        }
                    }                   
                }
        });     

“contor”是一个全局变量,我想从 processImobs() 访问它,但它的值仍然是 0,因为我猜它不会在整个回调中保留?有没有办法解决这个问题?

标签: javascriptgoogle-maps

解决方案


推荐阅读