在微信小程序中,用户点击按钮或控件时,如果响应较慢,会重复点击,多次调用的问题。
1、设置透明遮罩层
wx.showLoading({
title: message,
mask: true
});
2、使用节流
/**
* 点击跳转路径接口 == 节流
*/
goToPage: util.throttle(function (e) {
let that = this
that.goPage(e)
}),
/**
* 节流 函数节流是立马执行,n秒后再立马执行
* 函数节流(throttle):函数在一段时间内多次触发只会执行第一次,在这段时间结束前,不管触发多少次也不会执行函数。
* @param {*} fn 回调函数
* @param {string} gapTime 时间
*/
function 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.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime //赋值给第一次触发的时间,这样就保存了第二次触发的时间
}
}
}