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

265
Visualizações
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?

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

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

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>

about 4 years ago · Santiago Gelvez Relatório

0

Here is an example if the code not clear tell me in comment section, i will provide some more explanation if needed

const button = document.getElementById("hover")
let date = null
let timer
button.addEventListener("mouseover", function () {
    if(!date) date = new Date()
    timer = setInterval(()=>{
        if((new Date() - date) >= 4000) {
        console.log('Do Action')
        clearInterval(timer)
        }
    }, 1000)
}, false)

button.addEventListener("mouseout", function () {
    clearInterval(timer)
    date = null
})
<body>
    <button id='hover'>Hover Me</button>
</body>

about 4 years ago · Santiago Gelvez 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