简体   繁体   中英

How to detect a long press on a button/image

I am working on a phonegap project. I need to implement a long press event. How can we detect long press on a image/button using JavaScript?

$('#target').mousedown(function() {
  alert('Handler for .mousedown() called.');
  //start a timer
});

$('#target').mouseup(function() {
  alert('Handler for .mouseup() called.');
  //stop the timer and decide on long click

});

One way that comes in my mind is:

  1. In the start of the onclick event, record the time, this gives you the time of the first click.

  2. Then check the time span. Suppose, you say 5 seconds time span is a long press event. If check is success, this is a long press event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM