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

200
Vistas
Run the function until the mouse button is pressed in JavaScript

I want the function to run continuously as long as the user presses the mouse button But it runs only once and is printed once on the console I do not know how to solve this problem please guide me

  function handleBye() {
      return console.log("BYE");
    }
    function handleHello() {
      return console.log("HELLO");
    }


<button onmousedown="handleHello()">hello</button>
<button onmousedown="handleBye()">bye</button>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Maybe you are looking for setInterval and clearInterval:

let timer;

function stop() {
  clearInterval(timer);
}

function handleHello() {
  repeat(() => console.log("HELLO"));
}

function handleBye() {
  repeat(() => console.log("BYE"));
}

function repeat(what) {
  timer = setInterval(what, 200); // Schedule
  what(); // Also do it immediately 
}
<button onmousedown="handleHello()" onmouseup="stop()">hello</button>
<button onmousedown="handleBye()" onmouseup="stop()">bye</button>

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