Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda