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

89
Visualizações
How to prevent reactivate mousedown function before certain time

My objective is to activate a function repeatedly while mantaining click button and stop doing it when I stop clicking.

This part works fine: throught shoot(), I call shooting() for the first time and it runs printing "bullet" each 500ms until I release mouse button.

The problem is that if I click quickly again, "bullet" is printed as many times as I clicked, thus not respecting the 500ms frequence.

let fire = false;

document.addEventListener("mousedown", (e)=>{
    if((e.button === 0)) {
        fire = true;
        shoot();
    }
})

document.addEventListener("mouseup", ()=>{
        fire = false;
        shoot();
})

const shooting = () => {
    let timeId = setTimeout(()=>{
        if(fire) {
            console.log("bullet");
            shooting();
        }
        if(!fire) clearTimeout(timeId);
    }, 500);
}


const shoot= () => {
    shooting();
}

I tried a lot of ways and searched so much but I can not figure how to prevent to re-activate "bullet" without respecting the frequence of 500ms.

I am quite newbie so, if this has an obvious solution, please don't hold it against me.

Thanks you in advance. JM.

about 4 years ago · Juan Pablo Isaza
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