传单如何清除标记上的setTimeout

leaflet how to clearTimeout a setTimeout on a marker?

本文关键字:setTimeout 单如何 清除      更新时间:2023-09-26

我在地图上添加这样的标记:

 map.addLayer(l), setTimeout(function() {
      map.removeLayer(l)
 }, 1e4),

其在10秒之后再次移除每个标记。现在,我希望当用户在这10秒内点击一个标记时,市场在地图上保持可见。到目前为止,我有:

l.on('click', function(e) {
console.log(e);
console.log(e.layer._leaflet_id);
console.log(l);
clearTimeout(e.layer._leaflet_id);
});

但它现在确实起作用了。你知道我怎样才能做到这一点吗?

您需要通过使用相关ID调用clearTimeout来取消setTimeout。

    var myVar;
    timeout_init();
    function timeout_init() {
        myVar = setTimeout(function(){
            $('.marker').hide();
            },5000);
    }
$( ".marker" ).click(function() {
    clearTimeout(myVar);
});

参见Fiddle 示例