简体   繁体   English

如何停止待处理的ajax请求?

[英]How to stop pending ajax requests?

I am trying to stop pending ajax requests when a certain action happen on a LeafletJS map. 我试图在LeafletJS地图上发生某些操作时停止待处理的ajax请求。 Here is exactly what I am doing, I have a map ( that uses LeafletJS ) which queries a WMS service, when a user pan, zoom, or query a new layer, the previously queued ajax requests continue to wait for a response. 这正是我正在做的事情,我有一个查询WMS服务的地图(使用LeafletJS),当用户平移,缩放或查询新层时,先前排队的ajax请求继续等待响应。

The idea is to process only the requests that would serve the current map view criteria and cancel everything else. 这个想法是只处理将满足当前地图视图标准的请求,并取消其他所有请求。 The reason I am trying to achieve this is to improve the performance and the speed of rendering of the current action and ignore all previous actions. 我试图实现此目的的原因是为了提高当前动作的性能和渲染速度,并忽略所有先前的动作。

I feel like this is more of a Javascript question than a leafletJS specific question so I looked into abort() but i am not sure how to apply it with LeafletJS 我觉得这更多的是Javascript问题,而不是leafletJS特定问题,因此我调查了abort(),但是我不确定如何在LeafletJS中应用它

Edited: Here is the code I currently have to query the WMS service ( The service is private ) 编辑:这是我当前需要查询WMS服务的代码(该服务是private)

$scope.makeWMSLayer = function (layerInfo) {

            if (layerInfo == null)
                return false;

            var uri = apiUrl+"/WMSService/";
            var options = {
                layers: layerInfo.Name,
                maxZoom: layerInfo.maxZoom || 20,
                minZoom: layerInfo.minZoom || 11 ,
                format: 'image/jpeg',
                transparent: true
            };
            var layer = L.tileLayer.wms(uri, options);

            layer.on({
                load: function () { self.layersLoading[layerInfo.Name] = false; digest(); },
                loading: function () { self.layersLoading[layerInfo.Name] = true; digest(); }
            });

/*keep current active layer inside a $rootScope variable*/
            $rootScope.activeLayer = layer;

            return layer;
        };

If you are using jQuery for getting JSON data, you should use $.ajax instead of others (like $.getJSON etc.), and store it as a value. 如果您使用jQuery获取JSON数据,则应使用$.ajax而不是其他(例如$.getJSON等),并将其存储为值。

Like 喜欢

var jqXHR = $.ajax({ [...] }, [...]);

then just abort it: 然后中止它:

jqXHR.abort();

EDIT: 编辑:

TileLayer does not provide any ajax requests. TileLayer不提供任何ajax请求。 It's simply assigning a "src" attribute for tiles. 它只是为图块分配一个“ src”属性。

Unless you want to implement your own asynchronous (pre)loading by eg extending TileLayer, window.stop() is your only go. 除非您想通过扩展TileLayer来实现自己的异步(预)加载,否则window.stop()是唯一的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM