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

195
Views
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 answers
Answer question

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 Report
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!