uniapp input组件防抖

1.有问题的防抖版本:

<input type="text" value="" return-key-type="search" @input="replaceInput" class="fz30 inp_content" />


replaceInput(e) { // 每输入一个值 input事件都会触发,会导致timer一直都是空的,不会清空timer
	let timer = null
	console.log(timer);
	if(timer) {
		console.log('清除');
		clearTimeout(timer)
	}
	timer = setTimeout(()=>{
		console.log('执行啦');
		let val = e.detail.value
		console.log(val);
		timer = null
	},1000)
				
},

控制台输出:

2.解决问题的防抖版本:timer设置为全局

data() {
			return {
				timer: null
			};
		},

replaceInput(e) {
				console.log(this.timer);
				if(this.timer) {
					console.log('清除');
					clearTimeout(this.timer)
				}
				this.timer = setTimeout(()=>{
					console.log('执行啦');
					let val = e.detail.value
					console.log(val);
					this.timer = null
				},1000)
			},

控制台输出:

3.在h5中一定要使用v-model的方式

<input type="text" value="" @input="search" v-model="searchValue" return-key-type="search"
									:autofocus="isFocus" class="fz30 inp_content" />



search(e) {


					
						if (this.timer) {
							clearTimeout(this.timer)
						}
						this.timer = setTimeout(() => {
							console.log(this.searchValue);
							this.timer = null
							this.getSearchList(this.searchValue)
						}, 1000)
					
					},

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值