2014-09-23 36 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)"/> 
相關問題