angular - How to limit HTTP requests to every 2 seconds -
i'm using ionic 2 (angular 2) , have autocomplete makes api request every time word typed. change every 2 seconds unable using throttletime
, debouncetime
service.ts
search(keyword): observable<any> { let url = `${this.api}/products/${keyword}`; return this.authservice.refreshtoken() .flatmap(() => this.authhttp.get(url) .throttletime(10000) .debouncetime(10000) .map( (response: response) => { return response.json().products; }, (error: response) => { console.log(error); }) .share(); }
searchbar.html
<ion-searchbar [showcancelbutton]="true" [placeholder]="'search product'" [autocomplete] = "on" (input)="onsearch($event)"> </ion-searchbar>
component.ts
onsearch(event) { let keyword = event.target.value; this.searchproductsservice.search(keyword) .subscribe( (products: pinterface[]) => { this.products = products; }, (error: response) => { console.log(error); }); }
you can use observables in angular2 import react js library , call interval function. if didnt goof somthing else, should work:
import {http} '@angular/http'; import 'rxjs/rx'; import {observable} 'rxjs/rx'; observable.interval(2000) .switchmap(() => http.get('http://yoururl.com/products/')).map((products) =>products.json()) .subscribe( (products: pinterface[]) => { this.products = products; }, (error: response) => { console.log(error); });
Comments
Post a Comment