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

180
Visualizações
How not to get spammed with mouseover and mouseout (by time)?

https://jsfiddle.net/0bt3hy29/6/

function chandgeBackgroundImageOnMouse() {
    house.addEventListener("mouseover", ()=>{
        bgImage.style.backgroundImage = home
        is_paused = true
})
    house.addEventListener("mouseout", ()=>{
        is_paused = false
    })
}

When you mouseover the "house" element and then mouseout it - everything is working as should. But if you will mouseover and mouseout very quickly - auto slider will be broken.

So, i see 2 choises.

  1. To make react only after a gap of 3 seconds (it's mouseover and then mouseout - only after 3 seconds it can be mouseover one more time)

  2. To make react only when picture was changed (it's mouseover, then mouseout - and only after picture was changed it can be mouseover one more time)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code is recursive with interval so its recursvevly creating new instances of many things. Instead, try this.

    const car = "url('https://images.unsplash.com/photo-1580273916550-e323be2ae537?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80')"
const home = "url('https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1175&q=80')"
const candles = "url('https://images.unsplash.com/photo-1593542468714-ac3ba4b38d43?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1169&q=80')"
const data = [
car,
home,
candles,
]

const house = document.querySelector('#house')
const avto = document.querySelector('#avto')
const bgImage = document.querySelector('.img-of-dog')

let current = 0;

function intervalMethod(){
        bgImage.style.backgroundImage = data[current]
        current = ++current % data.length
}

let changeTimeset = setInterval(()=>{
        intervalMethod();
    }, 2000)
  
  
house.addEventListener("mouseover", ()=>{
     clearInterval(changeTimeset);
          bgImage.style.backgroundImage = home
    })
house.addEventListener("mouseout", ()=>{
     changeTimeset = setInterval(()=>{
        intervalMethod();
         }, 2000)
        })
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