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

128
Views
JS how to have keyDown for a limited amount of time

I'm trying to make a video game with Javascript, HTML and css, a platformer. But I encountered a difficulty with the eternal jump on 'w', I have a "gravity that get me down if I don't do anything. So I thought it was possible to get:

function up() {
  document.addEventListener("keydown", function (event) {
      if (event.key === "w" || event.key === "ArrowUp") {
          upKey = true;

but it must work only for x seconds so it's a limited jump, any ideas are welcomed. I tried with principe:

function execOnce(fn, context) {
  var result;
  return function () {
    if (fn) {
      result = fn.apply(context || this, arguments);
      fn = null;
    }
    return result;
  };
}

function sayHello() {
  console.log("hello world");
}

var helloOnce = execOnce(sayHello);

helloOnce(); // hello world
helloOnce(); // nothing but emptiness
helloOnce();
helloOnce();

But my variables are true and not a number so I don't know how to do. Thank you (yes I'm new to JS)

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!