Search code examples
javascriptjqueryonmouseoveruniversal-analytics

How to activate onmouseover after specific duration?


How is it possible to define a specific duration on onmouseover function ?

For example, after 500ms hover the element, onmouseover starts to run (so, if the user doesn't saty more than 500ms, onmouseover doesn't run).

In my case, I would like to apply an event (Universal Analytics).
If the user stays more than 500ms hover the element, the event will be activate.

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


Thanks.


Solution

  • Do not call function from HTML onclick if it can be avoided. Add event listener with jQuery. See http://www.quirksmode.org/js/events_early.html

    $('element').on('onmouseover', function(){
    
        setTimeout(function() {
          ga('send','event','title');
        }, 500);
    }