input输入时触发ajax请求事件 在这个时候如何实现函数防抖 searchInput(e){ this.setState({ searchContent:e.target.value
})
fetch(`/api/book/auto-complete?query=${e.target.value}`)
.then(res=>res.json())
.then(res=>{ console.log(res); this.setState({ completionArr:res.keywords
})
})
}
1 回答

桃花长相依
TA贡献1860条经验 获得超8个赞
加个定时器setTimeout
即可
searchInput(e){ this.setState({ searchContent:e.target.value }) this.timer && clearTimeout(this.timer); this.timer = setTimeout(() => { fetch(`/api/book/auto-complete?query=${e.target.value}`) .then(res=>res.json()) .then(res=>{ console.log(res); this.setState({ completionArr:res.keywords }) }) }, 500) }
添加回答
举报
0/150
提交
取消