Quiero que la función se ejecute continuamente siempre que el usuario presione el botón del mouse Pero solo se ejecuta una vez y se imprime una vez en la consola No sé cómo resolver este problema, por favor guíeme
function handleBye() { return console.log("BYE"); } function handleHello() { return console.log("HELLO"); } <button onmousedown="handleHello()">hello</button> <button onmousedown="handleBye()">bye</button>Tal vez esté buscando setInterval y 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>