Javascript registers onmousedown event only when onmouseup happens? (They behave as the same event..)

65 Views Asked by At

I am testing a short script that is supposed to register whether the mouse button is held down, however on all the browsers that I tested it both onmouseup and onmousedown events fire simultaneously - when mouse button is unpressed.

This is one example:

var check = 0;
document.onmousedown = function() {
  check++
}
document.onmouseup = function() {
  check--
}
setInterval(function() {
  if (check) {
    console.log(check)
  }
}, 1000 / 30);

Which is supposed to spam '1' in the console when the mouse is held down, however it does not work. And with this code:

document.onmousedown = function() { 
  console.log('down');
}
document.onmouseup = function() {
  console.log('up');
}

It is easy to see that nothing fires when the mouse is pressed, but both events fire when the mouse is released. What am I missing?

0

There are 0 best solutions below