Hedra
Hedra

Reputation: 89

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.

Upvotes: 0

Views: 350

Answers (2)

Russell Bevan
Russell Bevan

Reputation: 334

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);
}

Upvotes: 1

Vijaiendran
Vijaiendran

Reputation: 71

You can use a timeout method to send the event after given time.

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

Upvotes: 1

Related Questions