我有一个对象数组,对于数组的第一棵树元素,应将isChecked属性设置为true。数组的索引为3后,我应该重定向到另一页,但控制台日志仍在运行
public sendApplication(): void {
if (this.formService.isFormValid(this.formGroup)) {
this.dialogProcessing
= this.dialog.open(FoDialogBankVerificationComponent, {
width: '500px',
disableClose: true,
data: this.checkBoxValues,
});
this.submit()
.pipe(
take(1))
.subscribe(res => {
if (res.id === 1700) {
this.checkBoxValues.forEach((checkbox, index) => {
setInterval(() => {
checkbox.isChecked = true;
if (index === this.checkBoxValues.length - 1) {
this.status = res.id;
this.dialogProcessing.close();
}
}, index * 1000);
});
} else {
this.checkBoxValues.forEach((checkbox, index) => {
setInterval(() => {
console.log(index)
if (index !== 3) {
checkbox.isChecked = true;
} else {
this.dialogProcessing.close();
this.navigationService.navigateToDeniedPage();
}
}, index * 1000);
});
}
});
}
}
答案 0 :(得分:0)
之所以发生这种情况,是因为您没有清除间隔,
首先声明一个变量,例如
interval:any;
然后像这样分配setInterval并使用相同的方法执行clearInterval,
this.interval = setInterval(() => {
checkbox.isChecked = true;
if (index === this.checkBoxValues.length - 1) {
this.status = res.id;
clearInterval(this.interval);
this.dialogProcessing.close();
}
}, index * 1000);
同样,当您 不再需要它们。
让我知道这是否不能解决您的问题