如何检测超过2秒的触摸屏被按下?

时间:2015-04-22 15:58:19

标签: javascript jquery google-chrome

使用Google Chrome和Canary。

如何检测超过2秒的触摸屏? (使用鼠标可以工作,但实际触摸屏失败,在Surface pro 3上测试)

$(document).ready(function() {

  var clickstart;
  var clickstop;

  $(document).bind('contextmenu', function()  {
    console.log('Touch screen, has no right click.');
    return false;
  });

  $(document).mousedown( function() {
    return false; 
  });

  // With USB mouse pointer it works, but with real-touch screen its not working
  $('#vip_anytime').on('mousedown', function(e) {    
      clickstart = e.timeStamp;
  }).on('mouseup', function(e) {
      clickstop = e.timeStamp- clickstart;
      if(clickstop >= 2000) {
        console.log('>>> After 2 second of hold');
      } else { 
        console.log('>>> Before 2 second of hold');
      }
  });

});

1 个答案:

答案 0 :(得分:2)

尝试touchstarttouchend事件。

    $('#vip_anytime').on('touchstart', function(e) {    
        clickstart = e.timeStamp;
    }).on('touchend', function(e) {
        clickstop = e.timeStamp- clickstart;
        if(clickstop >= 2000) {
          console.log('>>> After 2 second of hold');
        } else { 
          console.log('>>> Before 2 second of hold');
        }
    });