2014-09-23 52 views
0

如何在onmouseover函数上定义一个特定的持续时间?

例如,在元素悬停500ms后,onmouseover开始运行(所以,如果用户不超过500ms,onmouseover不会运行)。
如何在特定的持续时间后激活onmouseover?

就我而言,我想申请一个活动(Universal Analytics)。
如果用户停留超过500ms悬停元素,事件将被激活。

<img class="element" src="images/example.gif" onmouseover="ga('send', 'event', 'title');"/> 


感谢。

回答

1

如果可以避免,不要从HTML onclick调用函数。用jQuery添加事件监听器。见http://www.quirksmode.org/js/events_early.html

$('element').on('onmouseover', function(){ 

    setTimeout(function() { 
     ga('send','event','title'); 
    }, 500); 
} 
1

您可以使用超时方法在给定时间后发送事件。

<img class="element" src="images/example.gif" onmouseover="setTimeout(function(){ga('send', 'event', 'title');},1000)"/> 
相关问题