vue Input输入如果过快,会导致请求接口报错,解决防抖动方法
一:遇到问题
<uni-easyinput v-model="lvvcode" placeholder="请输入分单号" @input="setInput" />
<view class="eve" v-for="(item, index) in HAWBInfo" :key="index" >{{ item }}</view>
input框实现搜索分单号功能,显现下面的数据展示,如果input输入过快,会导致接口请求错乱,数据展示的不对。
二: 解决方法
不管input输入的多快,只要拿到input最后输入的数值为准就好,拿到最后输入的值给个延迟,过500ms之后再展示搜索出来的数据
在 util.js中写入方法
// 函数防抖
export function debounce(fn, wait) {
let timeout = null;
wait = wait || 600;
return function () {
let that = this;
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(that);
}, wait);
}
}
在页面的methods方法中写
setInput(e) {
this.lvvcode = e
this.inputNum()
},
inputNum: debounce(function() {
console.log('输入内容:', this.lvvcode);
this.getHAWBInfo(this.lvvcode)
}, 500),
即可完美解决input输入过快的问题
吾日三省吾身,脚踏实地~

浙公网安备 33010602011771号