Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!