const sendState = {
requestCount : 0,
isBlocked : false,
blockDuration : 10000,
requestTimeFrame : 5000,
firstRequestTime : Date.now()
}
// 判断一段时间内是不是频繁请求数据
const lazyLoad = ()=>{
if (sendState.isBlocked) {
uni.showToast({ icon: 'none', title: '您的请求过于频繁! 请在10秒后再试' })
return
}
const currentTime = Date.now()
// 检查距离第一次请求是否超过了5秒
if (currentTime - sendState.firstRequestTime < sendState.requestTimeFrame) {
// 如果请求次数超出阈值,显示提示,并且阻止进一步的请求
sendState.requestCount += 1
if ( sendState.requestCount >= 5) { // 假设连续请求5次以上就触发提示
uni.showToast({ icon: 'none', title: '请求次数过多,请稍后再试!' })
// 设置为阻止状态
sendState.isBlocked = true
// 30秒后重置状态,允许再次发送请求
setTimeout(() => {
sendState.isBlocked = false
}, sendState.blockDuration)
// 复位请求计数和时间
sendState.firstRequestTime = currentTime
sendState.requestCount = 0
return
}
} else {
// 5秒已经过去,重置请求计数和时间
sendState.firstRequestTime = currentTime
sendState.requestCount = 1
}
// 发送数据
}