Get the duration of a touch in javascript

前端 未结 3 921
抹茶落季
抹茶落季 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: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.

提交回复
热议问题