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

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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