Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda