我用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() 仅在其自己的组件中工作?