0

I'd like to make it so onmousedown is repeatedly called if the left mouse button mouse is held down. Right now it only fires once when it is clicked.

    wHandle.onmousedown = function (event) {
        console.log('mouse button is being held down!'); // does not work
    };

This is how I'd like it to function (this works with space, it calls the function over and over as long as space is being held down):

    wHandle.onkeydown = function (event) {
        switch (event.keyCode) {
            case 32: // space
                console.log('space is being held down!'); // works!
            break;
    };
daniel metlitski
  • 747
  • 3
  • 11
  • 23
  • Possible duplicate of [JavaScript repeat action when mouse held down](https://stackoverflow.com/questions/1934986/javascript-repeat-action-when-mouse-held-down) – Samuil Petrov May 30 '17 at 10:44

2 Answers2

0

Maybe try using the addEventListener and calling the a function in the second parameter.

wHandle.addEventListener("mousedown", exampleFunction)

Also, haven't tested this so not 100% sure it will work.

0

You can try the below code snippet for getting the expected result

var interval;
window.onmousedown = function() {
    interval = setInterval(handleMouseDown, 1); 
};
window.onmouseup =function() {
    clearInterval(interval); 
};
function handleMouseDown() {
   console.log('mouse down');
}

Hope this helps!!!

Haroon
  • 111
  • 6