How to realize the function anti-shaking at this time when the ajax request event is triggered when input input

how to implement the function anti-shake at this time when the ajax request event is triggered when input input

    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
            })
        })
    }
Sep.16,2021

add a timer setTimeout to

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