小程序自定义双击事件

2019-07-17  本文已影响0人  前端来入坑
//js
......
let timer = null;
Page({
       data: {
        startTime: 0,
        clickNum: 0
      },
      doubleClick: function(e) {
        let that = this;
        let curTime = e.timeStamp;
        let startTime = this.data.startTime;
        if(startTime == 0){
          that.setData({
            startTime: curTime
          })
         timer = setTimeout(function(){
            that.resetClick();
            if(that.data.clickNum == 1){
              console.log("双击执行事件");
              that.setData({
                startTime: 0,
                clickNum: 0
              })
            }else{
              console.log("单击执行事件");
            }
            clearTimeout(timer);
          },300);
        }else{
          if(curTime - startTime < 300){
            this.setData({
              clickNum: 1
            })
          }
        }
      },
      resetClick: function(){
        let that = this;
        if (that.data.clickNum == 0) { 
          this.setData({
            startTime: 0,
            clickNum: 0
          })
        }
      },
})
......

使用

//wxml
<view catchtap="doubleClick"></view>
上一篇 下一篇

猜你喜欢

热点阅读