Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
javascript function only executed if certain element is NOT being hovered

Hi,

I want a js code to execute only if #onlineusers and its children are NOT being hovered. This is my attempt:

document.getElementById("onlineusers").onmouseout  = function() { setInterval (loadusers, 5000);}

it doesnt work since the function executes no matter what.

What is wrong?

Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your previous code was not working because if the user's mouse left the element, it would create an interval that never checked again whether the user was hovering over that element.

Instead, you can check whether the element is being hovered over by checking whether it matches the :hover CSS pseudo selector. This eliminates the need to listen for the mouseout event at all:

const toCheckIfHover = document.getElementById("demo");
setInterval(function(){
  if(!toCheckIfHover.matches(':hover')){
     console.log('not hovering over #demo');
  }else{
    console.log('hovering over #demo');
  }
}, 500)
#demo{
  width:100px;
  height:100px;
  border:1px solid;
  background-color:#ededed;
}
<div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!