Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda