let debounce = {
map:[],
isValidClick:function(key, wait=1000){
let item = this.getItem(key);
if(!item){
if(this.map.length >10){
this.map.shift()
}
this.map.push({key:key,tick:new Date().getTime()});
return true;
}else{
let now = new Date().getTime();
let result = now - item.tick > wait;
item.tick = now;
return result;
}
},
getItem:function(key){
for(let i=0;i<this.map.length;i++){
let item = this.map[i]
if(item.key === key){
return item
}
}
return null
}
}
module.exports = debounce
使用
const debounce = require("../../utils/debounce")
Page({
onAdd: function (e) {
let state = app.globalData.userInfo.state;
let isGetUserInfo = ((state & 1) === 1); //判断是否提交过用户信息
let isGetMobil = ((state & 2) === 2); //判断是否已绑定手机
if (!isGetUserInfo || !isGetMobil) {
if (!isGetUserInfo) {
wx.navigateTo({
url: '/pages/login/wxLogin/wxLogin?index=-1'
})
return;
}
if (!isGetMobil) {
wx.navigateTo({
url: '/pages/login/login?index=-1'
})
return;
}
}
if (debounce.isValidClick('game_on_add')) { ///////////////使用
wx.showModal({
title: '温馨提示',
content: '确定加入游戏吗?',
showCancel: true,
confirmColor: "#000",
success: (res) => {
if (res.confirm) {
this.onRequest()
}
}
})
}
},
})
保持队列里面只有10个事件 超过了干掉最先进来的;默认防抖时间是1000ms;
已存在就判断更新时间 没有就加进队列
简单明了 一看就懂 不做解释