Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Angular6: on field change if no activity for x amount of time then run function

My search field will run the following function on (change)="onSearch($event)"

    onSearch(q) {
        console.log('started search')
        const self = this;
        let timeout = null;
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            self._results = self.bcProductService.getProducts_packed({'keyword':q}).subscribe( (results)=> {
                _log(' == Results ==> ','bc', results.data);
                self.results = results.data;
            });
            self.searchText = q;
        }, 3000);
    }

as you can see. I am trying to only run the function once if no activity for 3 seconds. But it is actually running once for every (change) event. How can i write rewrite this function to only run once for every pause instead of every-time they type.

Comments