Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count time on hover with pure javascript?

I want when i hover on element to count 4 seconds for example and then do the action. But only if user really hover on that element 4 seconds, i dont want to use setTimeout function.

I found this example:

How to check how long you hover an element in pure javascript?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but this only count when user do mouseout. I want to do calculation on mouseover and if pass 4 seconds do something. Any suggestion?

>Solution :

As an alternative to the previous answer with setInterval

var handle = null

function enter() {
  handle = setTimeout(function() {
    alert('mouse in for 4s');
  }, 4000);
}
function out() {
  clearTimeout(handle);
}
<button onmouseenter="enter()" onmouseout="out()">test</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading