在开发的过程中,我们经常会碰到一个问题,按钮疯狂点击,节流或者防抖其实解决不了这类问题,所有才有了下面这个方法,亲测可用参考地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 忽略重复事件函数 参考地址:https://lequ7.com/guan-yu-qian-duan-you-ya-jie-jue-an-niu-chong-fu-dian-ji-wen-ti.html
* @param {执行函数} func
* @param {是否自动解锁,true为手动,false为自动} manual
*/
const ignoreMulti = (func, manual = false) => {
let lock = false;
return function (...args) {
if (lock) return;
lock = true;
let done = () => (lock = false);
if (manual) return func.call(this, ...args, done);
let promise = func.call(this, ...args);
Promise.resolve(promise).finally(done);
return promise;
};
}

自动解锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import regeneratorRuntime from "../../../../utils/runtime.js";
import { ignoreMulti } from "../../../../utils/util.js";
Page({
data: {
},
onLoad(options) {
this.handlerConfirm = ignoreMulti(this.handlerConfirm);
},

async handlerConfirm() {
if (!this.checkInfo()) {
return false
}
const { success, data, msg } = await createOrder(filterParams({
from: this.data.goodsId ? 1 : 0,
goodsId: this.data.goodsId || null,
quantity: this.data.num || null,
...this.data.catchStatus,
pickupTime: channel.data.time,
pickupReservedMobile: channel.data.mobile
}))
if (success) {
this.setData({
orderId: data.restockOrderId
})
wx.nextTick(() => {
this.getPayParams(data.restockOrderId)
})
} else {
errorToast(msg)
}
}
})

手动解锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import regeneratorRuntime from '../../../../utils/runtime.js';
import { ignoreMulti } from '../../../../utils/util.js';
Page({
data: {
},

onLoad() {
this.handleOrder = ignoreMulti(this.handleOrder, true);
},
async handleOrder(e, done) {
const arr = this.checkIsSelect()
if (arr === false) {
done();
return false;
}
const { success, msg } = await changeChecked({
goodsList: arr.map(x => x.goodsId)
})
if (success) {
// 跳转到下单页面
wx.navigateTo({
url: `/pages/stock/box/generateOrder/index`,
complete(){
//由于微信跳转有延时性需要延时解锁
setTimeout(done, 2000);
}
});
return false;
} else {
errorToast(msg);
done();
}
}
})