繁体   English   中英

在 API 返回 Angular 中的数据之前添加 sleep

[英]Adding sleep before API returned the data in Angular

我有一个 API 需要一些时间才能从 Db 返回数据,同时我收到 504 错误。 在得到响应之前有没有增加睡眠? 我尝试了下面的代码,但在几秒钟内得到了 504 问题。 请给我建议。

this.http
            .get("/testAPI/testAPIData", { params })
            .pipe(timeout(500000)) 
            .subscribe(
              (res) => {
                if (res.hasOwnProperty("errorMessage")) {
                  $("#idErrorMessage").html(
                    "<tr><td colspan='5'><b>No data available</b></td></tr>"
                  );             
                  this.dtTrigger.next();
                } else {
                  this.results = res;
                  this.dtTrigger.next();
                }
              },
              (err) => {
                console.log("Error While Loading the Table  ::: " + err.error);
              }
            );
        }

使用去抖时间()

我认为您正在尝试减少 HTTP 调用的数量

我将从 rxjs 向用户 debounceTime (1000)退款

例子

import {  debounceTime } from 'rxjs/operators';

...

this.http.get("/testAPI/testAPIData", { params })
            .pipe(debounceTime(1000)) 
            .subscribe(
              (res) => {
               ...
              },
              (err) => {
               ...
              }
            );
        }

此 function 将收集 stream 并在 1 秒后接听电话,而不是每个 stream。

有关更多信息,请阅读文档: https://rxjs-dev.firebaseapp.com/api/operators/debounceTime

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM