简体   繁体   English

Angular rxjs 去抖

[英]Angular rxjs debounce

Basically my issue is that when the connection is slow the user was able to press the save button multiple times and multiple data are being save.基本上我的问题是,当连接速度很慢时,用户能够多次按下保存按钮并且正在保存多个数据。 This issue does not occur locally but on staging it does.此问题不会在本地发生,但在暂存时会发生。

Although I already have set this.hasBeenSubmitted = true;虽然我已经设置了 this.hasBeenSubmitted = true; when the request is done and [disabled] on the button based on the condition user was still able to click the button multiple times and could save multiple times which is wrong.当请求完成并根据条件在按钮上[禁用]时,用户仍然能够多次单击按钮并且可以保存多次,这是错误的。

Some say that Angular rxjs debounce can be solution to this, can someone enlighten me regarding this one?有人说 Angular rxjs debounce 可以解决这个问题,有人能告诉我这个吗? Thank you.谢谢你。 And how would it help my issue based on the code below.根据下面的代码,它将如何帮助我解决问题。 Thanks.谢谢。

Code代码

save(): void {
    const create = this.requestFormService.createRequestData(this.form, this.respondents)
      .subscribe(
        (results: FeedbackRequest[]) => {
          this.hasBeenSubmitted = true;
        },
        (error) => {
          this.hasBeenSubmitted = false;
          this.handleInvalidFields(error, 'Failed to save the Feedback Request as draft. One or more fields contain invalid values. Input a valid value to proceed.');
          this.messageDialogService.show('Failed to save the Feedback Request as draft. One or more fields contain invalid values. Input a valid value to proceed.', true);
          create.unsubscribe();
        }
      );
  }

html html

<button [disabled]="hasBeenSubmitted"
            mat-raised-button *ngIf="form" (click)="save()" type="submit">
            <ng-container>
              <span>SAVE</span>
            </ng-container>
          </button>

At this point you set hasBeenSubmitted after the request is done.此时您在请求完成后设置 hasBeenSubmitted。 But since the request can take some time, the user can press the button again during this time.但由于请求可能需要一些时间,因此用户可以在此期间再次按下按钮。 You can set the flag before the data is being saved:您可以在保存数据之前设置标志:

save(): void {
    this.hasBeenSubmitted = true;
    const create = this.requestFormService.createRequestData(this.form, this.respondents)
      .subscribe(
        res => {},
        (error) => {
          this.hasBeenSubmitted = false;
          this.handleInvalidFields(error, 'Failed to save the Feedback Request as draft. One or more fields contain invalid values. Input a valid value to proceed.');
          this.messageDialogService.show('Failed to save the Feedback Request as draft. One or more fields contain invalid values. Input a valid value to proceed.', true);
          create.unsubscribe();
        }
      );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM