小编Yua*_*ong的帖子

当用户路由到不同页面时 setTimeout 继续,如何在角度打字稿中清除超时?

我用setTimeout在10秒后跳转到上传页面。但是当用户导航到另一个页面时,超时仍然存在。我按照此链接上的说明操作(Angular 2 setinterval() keep running on other component)并将clearTimeout添加到ngOnDestroy,但它不起作用。

  ngOnInit() {
    
    // Get uploaded file info
    this.uploadService.getFileInfo().subscribe((value) => {
      this.fileInfo = value;
    

      this.http.post(environment.apiUrl+'/api/dataValidation/validateData.php', this.fileInfo)
        .subscribe(res => {
          console.log('response'+res)
          if (res['length']<5) {
            
            //console.log(res);
            this.failure = true;
            this.fileValidationResults.errorMessage =res[0];
            this.loading = false; // stop loading screen
            this._timer =setTimeout(() => {
              this.router.navigate(["/uploads/upload"]);
            }, 10000); 
          }
        })
    });

  }
  ngOnDestroy() {
    if (this._timer){
      clearTimeout(this._timer);
  };
  }
Run Code Online (Sandbox Code Playgroud)

当用户导航到另一个链接时如何停止 setTimeout() 并让 setTimeout() 仅在其自己的组件中工作?

settimeout typescript cleartimeout angular

3
推荐指数
1
解决办法
1702
查看次数

标签 统计

angular ×1

cleartimeout ×1

settimeout ×1

typescript ×1