我是一个web开发和开发程序的初学者,在这个程序中,我使用传单通过ajax调用更新地图,它工作fine.Now,每10秒更新一次地图,所以我的地图更新功能如下:
function update_the_map(){
$.ajax({
type: "GET",
async : false,
url: 'http://backendserver:port/update,
success: function(data) {
var n=0;
for (j = 0; j<3000; j++) {
if(linestring['features'][j]['properties']['color']!=data[j].color){
linestring['features'][j]['properties']['color']=data[j].color;
}
}
if (vectorGrid) {
vectorGrid.remove();
console.log("removed the previous layer");
}
var vectorGrid = L.vectorGrid.slicer(linestring, {
rendererFactory: L.svg.tile,
vectorTileLayerStyles: {
sliced: function(properties, zoom) {
return {
weight: 2,
opacity: 1,
color: getColor(properties.color),
fillOpacity: 0.7
}
}
},
interactive: true,
getFeatureId: function(f) {
return f.properties.id;
}
})
.addTo(map);
console.log("updated the new layer");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
},
complete: function() {
if(time){
clearTimeout(time);
}
time= setTimeout(update_the_map, 10000);
}
});
}然后我将函数调用到
<script type="text/javascript">
window.onload = mapupdatecolor();
</script>但是在一段时间内,这个错误显示浏览器没有memory.So,当我查看相同的错误时,我认为它应该是超时函数的错误。但无法准确地找到错误occurs.Any帮助在何处得到赏识。
发布于 2017-04-28 17:05:44
在读了你的代码之后。浏览器耗尽内存的唯一原因似乎是L.vectorGrid.slicer的实例化。
在创建新实例之前,您应该尝试释放您删除的实例使用的内存。
这可能还不够,但您可以在vectorGrid.remove();之后执行此操作:
delete vectorGrid;如果它不能解决你的问题。寻找一种方法来清理vectorGrid创建的所有内容。
更新:
我刚刚注意到您的vectorGrid变量是在每个ajax成功函数调用中重新声明的,并且是一个调用的本地变量。这也可能是内存泄漏的原因。垃圾收集器可能认为这个局部变量从未无用,因此它不会“释放”内存。
以下是您可以尝试的:
// HERE IS A CHANGE
var vectorGrid;
function update_the_map(){
$.ajax({
type: "GET",
async : false,
url: 'http://backendserver:port/update',
success: function(data) {
var n=0;
for (j = 0; j<3000; j++) {
if(linestring['features'][j]['properties']['color']!=data[j].color){
linestring['features'][j]['properties']['color']=data[j].color;
}
}
if (vectorGrid) {
vectorGrid.remove();
// HERE IS A CHANGE
vectorGrid = undefined;
console.log("removed the previous layer");
}
// HERE IS A CHANGE
vectorGrid = L.vectorGrid.slicer(linestring, {
rendererFactory: L.svg.tile,
vectorTileLayerStyles: {
sliced: function(properties, zoom) {
return {
weight: 2,
opacity: 1,
color: getColor(properties.color),
fillOpacity: 0.7
}
}
},
interactive: true,
getFeatureId: function(f) {
return f.properties.id;
}
})
.addTo(map);
console.log("updated the new layer");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
},
complete: function() {
if(time){
clearTimeout(time);
}
time= setTimeout(update_the_map, 10000);
}
});
}这样,只有一个vectorGrid变量可以在每个update_the_map调用中释放内容。
https://stackoverflow.com/questions/43670218
复制相似问题