【问题标题】:Call function after setIntervalsetInterval 后调用函数
【发布时间】:2020-04-08 10:13:50
【问题描述】:

单击出现的弹出窗口后,我有提交按钮。弹出窗口有一个复选框列表,如果后端响应为 1700,则应检查这些复选框,然后弹出我们应该关闭。 但是调用关闭弹窗的函数是在 setInterval 完成之前调用的。

公共发送应用程序():无效{ 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((el, index) => {
                    setInterval(() => {
                        el.isChecked = true;
                    }, index * 1500);
                });
                this.status = res.id;
                this.dialogProcessing.close();
            }
        });
}

}

【问题讨论】:

  • 不确定您要完成什么,但看起来您正在搜索 setTimeout(cb:Function, time: number)

标签: angular setinterval


【解决方案1】:

异步编程,是指并行编程,其中一个工作单元彼此分开运行。

在您的情况下,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);
                    });

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多