仅在加载该页面(组件)时刷新特定页面

时间:2018-01-18 09:54:05

标签: angular

我需要在几分钟之后刷新一个页面,因为我正在使用setInterval并且在ngOnDestroy上,当我转到另一个页面时,我还在取消订阅,我看到数据仍在来自上一页。我该如何解决这个问题?

我的代码段



  ngOnInit() {
    setInterval(() => {
      if (
        this.showClearBtn === true ||
        this.showClrBtnStatus === true ||
        this.showClrBtnDate === true
      ) {
        console.log("do nothing");
      } else {
        this.getUsers();
      }
    }, 100000);  
  }
  
  
    getUsers() {
    this.sub = <Subscription>this.dataService.getUsers().subscribe(
      //   console.log('here ');
      data => {
        console.log(data);
        this.dataIsLoading = false;
        this.loading = false;
        switch (data["status"].resCode) {
          case 10001:
            console.log(data);
            this.customerList = data["pendingLoanApprovalArray"];
            console.log(this.customerList);
            this.customerList.forEach(element => {
              console.log(element.applicationDate);
              console.log(moment(element.applicationDate).format("MM/DD/YYYY"));
              element.applicationDate = moment(element.applicationDate).format(
                "Do MMMM YYYY"
              );
              console.log(element.applicationDate);
            });
            break;
          default:
            alert("something wrong");
        }
      },

      error => {
        console.log(error);
        if (error.status === 0) {
          this.router.navigate(["login"]);
          this.toastr.error("Invalid token!");
        }
        if (error.statusText === "Unknown Error") {
          //  this.dataIsLoading = false;
          //  this.toastr.error("Network Error!", "OOPS!");
        }
      }
    );
  }
  
    ngOnDestroy() {
    this.sub.unsubscribe();
  }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您必须按如下方式清除ngOnDestroy中的计时器:

this.timer = setInterval(() => {
                  //...
             }, 100000);  

在ngOnDestroy中,将其作为最后一个声明:

clearInterval(this.timer);