当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 函数防抖 解决重复点击消耗性能问题实现代码

微信小程序 函数防抖 解决重复点击消耗性能问题实现代码

2019年09月15日  | 移动技术网IT编程  | 我要评论

这篇文章主要介绍了微信小程序使用函数防抖解决重复点击消耗性能问题实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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 点击事件具体执行那个业务  
  }
 }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网