微信小程序使用函數防抖解決重復點擊消耗性能問題


wxml:

<view bindtap="doubleTap" bindtouchstart="touchStart" bindtouchend="touchEnd">click me</view>

   js:

 // 防止重復點擊
  touchStart(e) {
   
    this.touchStartTime = e.timeStamp;
  },
  touchEnd(e) {
    this.touchEndTime = e.timeStamp;
  },
  doubleTap(e) {
    var vm = this;
    // 控制點擊事件在350ms內觸發,加這層判斷是為了防止長按時會觸發點擊事件
    if (vm.touchEndTime - vm.touchStartTime < 350) {
      // 當前點擊的時間
      var currentTime = e.timeStamp;
      var lastTapTime = vm.lastTapTime;
      // 更新最后一次點擊時間
      vm.lastTapTime = currentTime;
      // 如果兩次點擊時間在300毫秒內,則認為是雙擊事件
      if (currentTime - lastTapTime > 300) {
        // do something 點擊事件具體執行那個業務
       
      }
    }
  }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM