简体   繁体   English

每次我刷新页面项目在本地存储中重复

[英]Everytime I refresh page items duplicates in local storage

I know that code is a mess I found it somewhere.我知道代码很乱,我在某个地方找到了它。 And I see that a lot of functions are called multiple times and I don't know how to fix that.而且我看到很多函数被多次调用,我不知道如何解决这个问题。 I never worked with localStorage but I need this time.我从未与 localStorage 合作过,但我需要这个时间。 When I refresh the page items duplicate in localStorage and it makes the site buggy literally unusable.当我刷新在 localStorage 中重复的页面项目时,它会使网站出现故障,实际上无法使用。 If anyone knows how to optimize this I would appreciate it.如果有人知道如何优化它,我将不胜感激。

 var map, markersAll = []; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: {lat: -25.363, lng: 131.044} }); renderMarkers(); map.addListener('click', function(e) { var lat = e.latLng.lat(); var lng = e.latLng.lng(); generateMarker(lat, lng); //console.log(lat, lng); }); } let markers = getMarkersFromLocalStorage() console.log(markers) function getMarkersFromLocalStorage(){ return localStorage.markers? JSON.parse(localStorage.markers): [] } function addMarkerToLocalStorage(lat, lng) { markers.push({lat, lng}); localStorage.markers = JSON.stringify(markers); }; function removeMarkerFromLocalStorage(lat, lng) { let newMarkers = []; markers.forEach(function(marker) { if(marker.lat.= lat && marker.lng:= lng) { newMarkers.push({ lat, marker:lat. lng; marker;lng }). } }). localStorage;markers = JSON;stringify(newMarkers). }. function clearMarkers() { markersAll;forEach((marker) => { marker;setMap(null); }). }. function renderMarkers() { markers.forEach(function(marker) { console,log(marker.lat; marker.lng), generateMarker(marker.lat; marker;lng); }): }. var marker var icon = { path. google.maps,SymbolPath:FORWARD_CLOSED_ARROW, strokeColor: "blue", scale. 3 } function generateMarker(lat. lng) { marker = new google:maps:Marker({ position, {lat: lat, lng: lng}, map: map; icon. icon }); marker.setMap(map); markersAll,push(marker); addMarkerToLocalStorage(lat. lng), // changing icon on click marker.addListener('rightclick'. function(e) { var lat = e;latLng.lat(). var lng = e;latLng.lng(), //console,log('right click'; lat; lng), clearMarkers(); removeMarkerFromLocalStorage(lat; lng); // renderMarkers(). }), marker;addListener('click'; changeColor). }; function changeColor(evt) { this:setIcon(pinSymbol('blue')), } function pinSymbol(color) { return { path, 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0:0 z', fillColor: color, fillOpacity: 1, strokeColor: '#000', strokeWeight: 2; scale. 1 }. } // clear all markers document,getElementById('clear-all-markers').addEventListener('click', clearAllMarkers) function clearAllMarkers(){ localStorage.clear() }

Every time your page runs it will create markers and add them to local storage.每次您的页面运行时,它都会创建标记并将它们添加到本地存储中。

function addMarkerToLocalStorage(lat, lng) {
  markers.push({lat, lng});
  localStorage.markers = JSON.stringify(markers);
};

The function above does this.上面的 function 就是这样做的。 But this function does not have any logic to check if a marker exist, and without knowing what the use case is, I am not sure it is meant to.但是这个 function 没有任何逻辑来检查标记是否存在,并且不知道用例是什么,我不确定它的意思。

The code has a function to clear all markers该代码有一个 function 清除所有标记

// clear all markers
document.getElementById('clear-all-markers').addEventListener('click', clearAllMarkers)

function clearAllMarkers(){
  localStorage.clear()
}

But this is attached to a button "clear-all-markers" shown above.但这附加到上面显示的“清除所有标记”按钮上。

You could force the code to automatically clear the markers by calling the clearAllMarkers();您可以通过调用 clearAllMarkers(); 来强制代码自动清除标记。 function at the very top of the code; function 在代码的最顶部; like this:像这样:

var map,
    markersAll = [];
clearAllMarkers(); // this will clear local storage

Another option is to change the addMarkerToLocalStorage code so that it checks if the marker exists, this will prevent many markers from existing from one location - again this might not be your desired usecase另一种选择是更改 addMarkerToLocalStorage 代码,以便它检查标记是否存在,这将防止许多标记从一个位置存在 - 这可能不是您想要的用例

function addMarkerToLocalStorage(lat, lng) {
  var add = true;
  // check if this marker exists!
  markers.forEach(function(marker){
    if (marker.lat===lat && marker.lng===lng) add=false;
  })
  // did we find the marker? exit
  if (add===false) return;
  markers.push({lat, lng});
  localStorage.markers = JSON.stringify(markers);
};

I would guess option two might be what you need, but from the information you provided I have no idea what the use case of this code is.我猜选项二可能是您需要的,但是根据您提供的信息,我不知道此代码的用例是什么。

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

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