1

I'm firing an onClick event and would like to somehow check if it was just a click or if it was held down for some time before releasing mouse button and actually firing a click event.

reason for this is to either perform a myTest() function so onClick="myTest()" that simply console logs either "mouse was clicked" or "mouse was held and clicked" depending on what action user performs.

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • 1
    This answer should help you: http://stackoverflow.com/a/19202926/4033878 :) – GtAntoine Mar 16 '16 at 10:31
  • Just a thought, perhaps you should use `onMouseDown` and `onMouseUp` instead of `onClick`. In the `mouseDown` event, get the time, then check the time again on `mouseUp` and compare the difference. Voila! You have the time the user held the click for. – Redtama Mar 16 '16 at 10:32
  • Possible duplicate of [What is the better way to get mouse hold time?](http://stackoverflow.com/questions/19201215/what-is-the-better-way-to-get-mouse-hold-time) – Redtama Mar 16 '16 at 10:38

1 Answers1

3

You should do below code:

var timeout, clicker = $('#clicker');
var count = 0;

clicker.mousedown(function(){
    timeout = setInterval(function(){
        clicker.text(count++);
    }, 500);

    return false;
});

$(document).mouseup(function(){
    clearInterval(timeout);
    return false;
});

You can hold the mouse on the square and the enter code here count interval is 500 miliseconds.

you can change it as per your requirements

Hope this will help you.

Kothari Ankit
  • 296
  • 1
  • 4
  • 12