How do you make markers disappear after a certain amount of time on leaflet map

Viewed 26

Using this code to let users mark where they see things on leaflet map but I want the markers to disappear after an hour. Is there a way I can set a time limit on them?

function getClickedLanLon(e) {

    var lat,
        lon,
        zoom;

    lat = e.latlng.lat;
    lon = e.latlng.lng;
    zoom = map.getZoom();

    marker2 = new L.Marker(new L.LatLng(lat, lon));
    map.addLayer(marker2);
    marker.bindPopup("Cat Spotted").addTo(map);

}

map.on('click', getClickedLanLon);
1 Answers

try setTimeout() built-in function in javascript.

let removeMarkerTimeOut = setTimeout({
  merker.removeFrom(map)
  }, 3600000
)

Related