react - while mouse is down javascript

JavaScript repeat action when mouse held down (3)

Is there to have a JavaScript function repeated every so many milliseconds that an html button is held down? It would be great if this could be done with standard JavaScript, but using jQuery or a jQuery plugin would be great too.

I see a problem with both solutions listed above.

onmouseup is only triggered if the mouse is released while it is over the button. If the user keeps the mouse down then moves the mouse away before releasing it then clearInterval is never fired and so do_something will fire forever.

You would need to add another event, "onmouseout", that also calls clearInterval.

I would use the javascript function setInterval() within a function that gets called on mouse down.

<input type="button" id="button" onmousedown="inter=setInterval(startAction, 1*1000);"
onmouseup="clearInterval(inter);" value="click here" />

<script type="text/javascript">
    function startAction(){
        //whatever you want done

var intervalId;
$("#button").mousedown(function() {
  intervalId = setInterval(do_something, 500);
}).mouseup(function() {
}).mouseleave(function() {
//this should help solve the problem that occurs when the mouse leaves the button while pressed down

function do_something() {
  // whatever