Get the duration of a touch in javascript

前端 未结 3 912
抹茶落季
抹茶落季 2020-12-17 04:35

Is it possible to get the duration of a touch using javascript on ios devices (and others if possible)? I would like to differentiate a long press from a short \"click\" pre

相关标签:
3条回答
  • 2020-12-17 04:44

    You can check the time to identify Click or Long Press [jQuery]

    function AddButtonEventListener() {
    try {
    var mousedowntime;
    var presstime;
    $("button[id$='" + buttonID + "']").mousedown(function() {
        var d = new Date();
        mousedowntime = d.getTime();
    });
    $("button[id$='" + buttonID + "']").mouseup(function() {
        var d = new Date();
        presstime = d.getTime() - mousedowntime;
        if (presstime > 999/*You can decide the time*/) {
            //Do_Action_Long_Press_Event();
        }
        else {
            //Do_Action_Click_Event();
        }
    });
    }
    catch (err) {
    alert(err.message);
    }
    } 
    
    0 讨论(0)
  • 2020-12-17 04:49

    Take a look at jQuery mobile http://jquerymobile.com/demos/1.0a4.1/#docs/api/events.html

    It has tap, and taphold events, which sounds like it might fit your needs.

    0 讨论(0)
  • 2020-12-17 04:51
    $('#element').each(function() {
    
       var timeout,
           longtouch;
    
       $(this).bind('touchstart', function() {
          timeout = setTimeout(function() {
              longtouch = true;
          }, 1000);
       }).bind('touchend', function() {
           if (longtouch) {
              // It was a long touch.
           }
           longtouch = false;
           clearTimeout(timeout);
       });
    
    });
    

    jsFiddle.

    The fiddle works with a long click.

    0 讨论(0)
提交回复
热议问题