I need to recognize a long click in a JavaScript bookmarklet. So, I cannot use jQuery, neither onclick() event and similar. Is it possible, and how?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Late reply, but instead of click / long click to provide two different actions, you might want to consider click / double click.
First click: Record time and then timer to perform action1 in 500 miliseconds.
Second click: If time since last click is short, cancel timer and perform action2. If time since last click is long, then this is a first click.
Nothing stops you from using triple click, etc.
Isn't a long click just a click where
mousedown
andmouseclick
events are considerably long away from each other? In order to solve that you could just measure the time it takes from amousedown
event to the click event and check if it is, e.g. longer than two seconds (or whatever you desire).You can access the current milliseconds since 01.01.1970 via
new Date().getTime()
. Given that I would intuitively check a "long click" like that.onmousedown
, callsetTimeout()
for the duration of your long click. If the timeout is allowed to expire, it will call its function to do whatever you hoped to do on the long click. However,onmouseup
you cancel thesetTimeout()
if it has not yet expired.Here it is in action in jsfiddle