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

225
Views
Run function when keyboard event is disable

I want to run the top set Interval whenever I removed my finger from space but when I use key up event it only run that function one time and I Dont know how to use if/else when I have add Event Listener

setInterval(function (e) {
  r--;
}, 10);

document.addEventListener("keydown", function (e) {
  console.log(r);
  if (e.keyCode === 32) {
    setInterval(function (e) {
      r++;
      if (r > 240) {
        r = 200;
      }
    }, 100);
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm assuming you want to do the function for x amount of times, when space is pressed down. If so you might wanne look at the code below. This will do a function for 100 times.

var isDoingStuff = false;
var r = 0;

function repeat(func, times) {
    func();
    times && --times && repeat(func, times);
}

function yourFunction(){
    r++;
    if(r > 240){
        r = 200;
    }
}

function onKeyDown(e){
    if (!isDoingStuff && e.keyCode === 32) {
      isDoingStuff = true;
      console.log(r);
      repeat(yourFunction, 100);
      isDoingStuff = false;
    }
}
document.addEventListener("keydown", onKeyDown);

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