Naxi
Naxi

Reputation: 2026

Onload function keeps adding to the array without clearing it

Might be a silly question. I have below js code in my jsp. The issue is that though I am trying to clear the array markerLong,markerLat on each load, it's not clearing them and keeps appending elements when I refresh the page.

<script>


// Gmaps Maps
// ------------------------------

$(window).on("load", function(){
// Map Markers
    // ------------------------------

    map = new GMaps({
        div: '#markers',
        lat: 18.520430,
        lng: 73.856743,
        styles: [{"featureType":"administrative.locality","elementType":"all","stylers":[{"hue":"#2c2e33"},{"saturation":7},{"lightness":19},{"visibility":"on"}]},{"featureType":"landscape","elementType":"all","stylers":[{"hue":"#ffffff"},{"saturation":-100},{"lightness":100},{"visibility":"simplified"}]},{"featureType":"poi","elementType":"all","stylers":[{"hue":"#ffffff"},{"saturation":-100},{"lightness":100},{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":31},{"visibility":"simplified"}]},{"featureType":"road","elementType":"labels","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":31},{"visibility":"on"}]},{"featureType":"road.arterial","elementType":"labels","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":-2},{"visibility":"simplified"}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"hue":"#e9ebed"},{"saturation":-90},{"lightness":-8},{"visibility":"simplified"}]},{"featureType":"transit","elementType":"all","stylers":[{"hue":"#e9ebed"},{"saturation":10},{"lightness":69},{"visibility":"on"}]},{"featureType":"water","elementType":"all","stylers":[{"hue":"#e9ebed"},{"saturation":-78},{"lightness":67},{"visibility":"simplified"}]}]
    });
    let markerLat,markerLong; 
    markerLat = [
        <c:forEach var="s" items="${list}">
            <c:out value="${s.currentLat}"/>,
        </c:forEach>
    ];
    markerLong = [
      <c:forEach var="s" items="${list}">
          <c:out value="${s.currentLong}"/>,
      </c:forEach>
    ];
      console.log(markerLong.length);
      console.log(markerLat.length);


markerLong=[];
markerLat=[];
});
</script>

How do I ensure that on every page refresh, the old entries are removed from the arrays.

Upvotes: 0

Views: 33

Answers (2)

Naxi
Naxi

Reputation: 2026

As i said, i did a very silly mistake. Js is working all fine. Its the java controller which keeps adding elements to the list here.

Upvotes: 0

Cody Tracey
Cody Tracey

Reputation: 11

You could try calling the function below right after the page loads.

function empty(){
markerLong.length = 0;
markerLat.length = 0
}

Upvotes: 1

Related Questions