我正在使用Ionic 2(Angular 2),並且我有一個自動完成功能,每次鍵入一個單詞時都會發出一個API請求。我想改變這種狀況,以每2秒,但我用throttleTime
和debounceTime
如何限制HTTP請求到每2秒
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 for a 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);
});
}
所以做你現在希望它每隔2秒發送一個請求**不管用戶是在輸入什麼東西? – 0mpurdy
不僅在用戶鍵入內容後(所以當在'search()'中產生http請求時) – Anonguy123