简体   繁体   English

传单javascript-如何清除标记上的setTimeout?

[英]leaflet javascript - how to clearTimeout a setTimeout on a marker?

I am adding markers like this to my map: 我在地图上添加了这样的标记:

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

which removes after 10 seconds each marker again. 每个标记10秒后会删除。 Now I would like to achieve that when the user clicks during those 10 seconds on a marker that the market stays visible on the map. 现在,我想实现以下目的:当用户在那10秒钟内单击在市场上保持可见的标记时。 So far I have: 到目前为止,我有:

l.on('click', function(e) {

console.log(e);
console.log(e.layer._leaflet_id);
console.log(l);

clearTimeout(e.layer._leaflet_id);

});

But it does now work. 但是它现在可以工作了。 Any idea how I can achieve this? 知道我该如何实现吗?

You need to cancel the setTimeout by calling the clearTimeout using the relevant ID. 您需要通过使用相关ID调用clearTimeout来取消setTimeout。

    var myVar;
    timeout_init();

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

$( ".marker" ).click(function() {
    clearTimeout(myVar);
});

See example Fiddle 参见小提琴示例

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

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