搜索延遲react
日常在項目中,常常會遇到搜索請求後臺的狀況,此時搜索延遲就顯得尤其重要,若是沒有搜索延遲功能,那麼用戶頁面將會顯得特別卡頓this
import {Component} from 'react' class Search extends Component{ constructor(props){ this.state({delay:0}); this.onSearch = this.onSearch.bind(this); } onSearch(){ this.setState({delay:this.state.delay + 1}) let _this = this ; setTimeout(function(){ _this.setState({delay:_this.state.delay - 1}); if(this.state.delay == 0){ //執行後臺請求的代碼 console.log('請求執行了') } } },1000) } render(){ return ( <div> <input onChange={this.onSearch}/> </div> ) } } export default Search
以上就是一個最簡單的搜索延遲功能的實現。有誤之處還請指出。code
jimwmg@foxmail.cominput