在app.js中写入公用方法throttle()
App({
globalData:{
},
onLaunch(options) {
// 第一次打开
},
onShow(options) {
// 从后台被 scheme 重新打开
},
throttle(fn, gapTime) {//防止多次点击跳转
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function () {
let _nowTime = + new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
//fn()如果直接调用,this指向会发生改变,所以用apply将this和参数传给原函数
fn.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime
}
}
}
});
在axml中写入
<button onTap="payProduct">闪付</button>
在js中写入
const app = getApp();
Page({
data: {},
onLoad(query) {
},
payProduct: app.throttle(function (e) {
app.payment(e.currentTarget.dataset.id);
}, 1000),
onReady() {
// 页面加载完成
},
onShow() {
// 页面显示
},
onHide() {
// 页面隐藏
},
onUnload() {
// 页面被关闭
},
onPullDownRefresh() {
// 页面被下拉
},
onReachBottom() {
// 页面被拉到底部
},
});