Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: long click for a bookmarklet

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?

like image 274
tic Avatar asked Feb 23 '23 05:02

tic


1 Answers

onmousedown, call setTimeout() 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 the setTimeout() if it has not yet expired.

<script type='text/javascript'>
// t will hold the setTimeout id
// Click for 1 second to see the alert.
var t;
</script>

<button onmousedown='t=setTimeout(function(){alert("hi");}, 1000);' onmouseup='clearTimeout(t);'>Clickme</button>

Here it is in action in jsfiddle

like image 67
Michael Berkowski Avatar answered Feb 27 '23 16:02

Michael Berkowski