Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 取消以前对Google Maps地理编码方法的调用_Javascript_Ajax_Google Maps_Callback_Google Geocoder - Fatal编程技术网

Javascript 取消以前对Google Maps地理编码方法的调用

Javascript 取消以前对Google Maps地理编码方法的调用,javascript,ajax,google-maps,callback,google-geocoder,Javascript,Ajax,Google Maps,Callback,Google Geocoder,我正在用Javascript和GoogleMaps地理编码实现一个自动完成方法(在输入时使用keyup事件),有时我会在实际搜索之前收到上一次搜索 我的意图是取消上一个电话,如果它还没有结束。有人能帮我吗 getAddressLatLng : function(text,callback) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({ address : text,

我正在用Javascript和GoogleMaps地理编码实现一个自动完成方法(在输入时使用keyup事件),有时我会在实际搜索之前收到上一次搜索

我的意图是取消上一个电话,如果它还没有结束。有人能帮我吗

getAddressLatLng : function(text,callback) {
    var geocoder = new google.maps.Geocoder();
     geocoder.geocode({
         address : text,
         region : Utils.getRegion()
     },
     function(results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             callback(results);
         }
     });
},

您可以创建一个全局请求计数器,并且仅在不增加的情况下执行回调行为。e、 g

var requests = 0;
...
    getAddressLatLng : function(text,callback) {
        var requestNum;
        requests++;
        requestNum = requests;
        var geocoder = new google.maps.Geocoder();
         geocoder.geocode({
             address : text,
             region : Utils.getRegion()
         },
         function(results, status) {
             if(requestNum != requests) return;
             if (status == google.maps.GeocoderStatus.OK) {
                 callback(results);
             }
         });
    },

我还建议取消/限制您的功能,这样您就不会浪费带宽来生成不必要的请求()。

使用关键事件来开始地理编码不是个好主意,这可能很快就会达到您的配额。