longPress.js 417 B

12345678910
  1. $.fn.longPress = function(fn) {
  2. var timeout = undefined;
  3. var $this = this;
  4. $this[0].addEventListener('touchstart', function(event) {
  5. timeout = setTimeout(fn, 800); //长按时间超过800ms,则执行传入的方法
  6. }, false);
  7. $this[0].addEventListener('touchend', function(event) {
  8. clearTimeout(timeout); //长按时间少于800ms,不会执行传入的方法
  9. }, false);
  10. }