如何在两秒钟悬停后执行功能?

Des*_*est 5 javascript google-maps function

这是我目前的代码

google.maps.event.addListener(marker, `mouseover`, function() { 
  alert('loaded when i hovered');
});
Run Code Online (Sandbox Code Playgroud)

但是如果鼠标在元素上方两秒钟,我想要执行该函数.

我尝试了这个,但它没有用.

 google.maps.event.addListener(marker, `mouseover  2000`, function() { 
   alert('loaded after then when i stay mouse 2 sec'); 
 });
Run Code Online (Sandbox Code Playgroud)

在两秒悬停后,我需要做什么才能使功能执行?

And*_*rey 8

You need to use a timer. Set it in mouseover, then in the timer callback do your work; also you need to handle a mouseout event where you stop the timer.

var timeoutId = null;
google.maps.event.addListener(marker, 'mouseover',function() { 
   timeoutId = window.setTimeout(function(){
     alert("I did it!");
   }, 2000);
 } );

// Cancel your action if mouse moved out within 2 sec
google.maps.event.addListener(marker, 'mouseout',function() { 
  window.clearTimeout(timeoutId)
});
Run Code Online (Sandbox Code Playgroud)