长按功能实现

以下代码是在vue中开发的

变量:

longClick: 0,  // 长按标志
timeOutEvent: 0,  // 计时器

方法:

start () {
      console.log('++++++++++start++++++++++++')
      var that = this;
      this.longClick = 0;  // 初始化
      this.timeOutEvent = setTimeout(function () {
        that.longClick = 1;  //  长按标志位
        // 此处为长按事件
        console.log('长按事件')
        // 生成画布并下载到本地
        that.createPoster()
      }, 500)
    },
    move (e) {
      clearTimeout(this.timeOutEvent);  // 清除计时器 
      this.timeOutEvent = 0;  // 清除标志位
      e.preventDefault();  // 阻止其他点击事件
    },
    end () {
      console.log('++++++++++end++++++++++++')
      clearTimeout(this.timeOutEvent);  // 清除计时器
      if (this.timeOutEvent != 0 && this.longClick == 0) {  // 判断是否非长按事件
        //此处为点击事件
        console.log('点击事件')
      }
      return false;
    },
原文地址:https://www.cnblogs.com/meiyanstar/p/15067114.html