在日常开发中有很多场景我们都需要用到节流函数和防抖函数,比如:实现输入框的模糊查询因为需要轮询ajax,影响浏览器性能,所以需要用到节流函数;实现手机号、姓名之类的的验证,往往我们只需要验证一次,这个时候我们就需要用到防抖函数;但是网上的很多资料都是不够具体和便于理解。
基本代码如下
model.trim="keyword">
查看
删除
基本代码如下
import _ from ‘lodash‘
export default{
computed:{
searchContent() {
return this.keyword
},
watch: {
// 如果 `question` 发生改变,这个函数就会运行
searchContent: function(newQuestion, oldQuestion) {
this.userListData.pageNumber = 1
this.getAccountUserListDebounce()
}
},
getAccountUserListDebounce: _.debounce(
function() {
this.getList()
},
// 这是我们为判定用户停止输入等待的毫秒数
500
),
getList() {
this.loading = true
mytomer(this.keyword, this.userListData.pageSize, this.userListData.pageNumber, null).then(res => {
this.loading = false
this.userListData.total = res.data.total
this.userListData.rows = res.data.rows
}).catch(error => {
this.loading = false
})
},
}
}
原文:https://www.cnblogs.com/smart-girl/p/12605728.html