猿问

input输入时触发ajax请求事件 在这个时候如何实现函数防抖

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
            })
        })
    }


炎炎设计
浏览 1500回答 1
1回答

桃花长相依

加个定时器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)     }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答