setInterval 后调用函数
Call function after setInterval
点击出现的弹出窗口后,我有提交按钮。弹出窗口有一个复选框列表,如果后端响应为 1700 则应该选中这些复选框,然后弹出我们应该关闭。
但是调用关闭弹窗的函数是在setInterval完成之前调用的。
public sendApplication(): void {
如果 (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((el, index) => {
setInterval(() => {
el.isChecked = true;
}, index * 1500);
});
this.status = res.id;
this.dialogProcessing.close();
}
});
}
}
异步编程,意味着并行编程,其中一个工作单元与另一个工作单元分开运行。
在您的情况下,this.status = res.id;
和 this.dialogProcessing.close();
不会等到 forEach 语句结束。
如何解决这个问题 => 例如,您可以将两个影响移动到 setInterval bloc 中,并验证它是否是执行它们的最后一个索引
像这样:
if (res.id === 1700) {
this.checkBoxValues.forEach((el, index) => {
setInterval(() => {
el.isChecked = true;
if(index=== this.checkBoxValues.length - 1){
this.status = res.id;
this.dialogProcessing.close();
}
}, index * 1500);
});
}
点击出现的弹出窗口后,我有提交按钮。弹出窗口有一个复选框列表,如果后端响应为 1700 则应该选中这些复选框,然后弹出我们应该关闭。 但是调用关闭弹窗的函数是在setInterval完成之前调用的。
public sendApplication(): void { 如果 (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((el, index) => {
setInterval(() => {
el.isChecked = true;
}, index * 1500);
});
this.status = res.id;
this.dialogProcessing.close();
}
});
}
}
异步编程,意味着并行编程,其中一个工作单元与另一个工作单元分开运行。
在您的情况下,this.status = res.id;
和 this.dialogProcessing.close();
不会等到 forEach 语句结束。
如何解决这个问题 => 例如,您可以将两个影响移动到 setInterval bloc 中,并验证它是否是执行它们的最后一个索引
像这样:
if (res.id === 1700) {
this.checkBoxValues.forEach((el, index) => {
setInterval(() => {
el.isChecked = true;
if(index=== this.checkBoxValues.length - 1){
this.status = res.id;
this.dialogProcessing.close();
}
}, index * 1500);
});
}