The updateList function is called each time the input field changes, taking event as an argument. How to reset the reqFn request interval inside updateList, for cases when the input field changes more often than 1000 ms?

updateList = (e) => { let query = e.target.value; let reqFn = function(){ axios.get(`https://typeahead-js-twitter-api-proxy.herokuapp.com/demo/search?q=${query}`) .then((response) => { //console.log(response); let getRq = this.props.onUpdateList.bind(this, response.data); getRq(); // or: //this.props.onUpdateList(response.data); }) .catch((error) => { console.log(error); }); }.bind(this); setTimeout(reqFn, 1000); } 

Trying to achieve a similar behavior: https://twitter.imtqy.com/typeahead.js/

  • maybe it's better to hang the eventlistener 'input', instead of a timeout? - NeedHate
  • one
    @NeedHate, in fact, updateList is this subscriber. Timeout to ensure that the server requests not spam. - Qwertiy

1 answer 1

 updateList = (e) => { ... clearTimeout(this.timeout); this.timeout = setTimeout(reqFn, 1000); }