接触小程序之后的遇到的一个问题当网络较慢时,用户连续点击多次按钮跳转页面;会出现栈堆多个;在网上找到两个解决的方法,大家可以参考一下;
1.类似于开关思想(锁)
flag默认为true
这个方法思路虽然是可以实现的。但是总感觉是不完美的,可能一会就出bug了;
2. 方法二。从网上参考,非常棒,(节流思想)
2.1先封装如下代码;
export function debounce(func, wait, immediate) {
let timeout, args, context, timestamp, result
const later = function() {
// 据上一次触发时间间隔
const last = +new Date() - timestamp
// 上次被包装函数被调用时间间隔last小于设定时间间隔wait
if (last < wait && last > 0) {
timeout = setTimeout(later, wait - last)
} else {
timeout = null
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
if (!immediate) {
result = func.apply(context, args)
if (!timeout) context = args = null
}
}
}
return function(...args) {
context = this
timestamp = +new Date()
const callNow = immediate && !timeout
// 如果延时不存在,重新设定延时
if (!timeout) timeout = setTimeout(later, wait)
if (callNow) {
result = func.apply(context, args)
context = args = null
}
return result
}
}
使用
导入
import {debounce} from "@/utils/utils.js"
html
<view class="nav-itemView" @click="MyOrder">我的订单</view>
js
MyOrder:debounce(
function() {},1000,true),
搞定,这样既解决了