I want to add some delay in making network request when I type in search bar. So I can prevent unnecessary network request.
header.component.ts
searchProduct(event: KeyboardEvent) {
if (event) {
const { value } = event.target as HTMLInputElement;
this.productService.searchProducts(value).subscribe((res) => {
console.log(res);
if (res.length > 5) {
res.length = 5;
}
this.searchResult = res;
});
}
product.service.ts
searchProducts(query: string) {
return this.http.get<Prodcut[]>(`http://localhost:3000/products?q=${query}`);
}