【发布时间】:2021-06-14 14:21:29
【问题描述】:
我有一个拨动开关,当它被触发时会发出一个 http PATCH 请求。您可以在下面看到它的样子:
模板:
<div id="toggle-status-btn-container">
<div class="slider" [ngClass]="{'to-right': !shopIsOpen}"></div>
<div class="label-container">
<label class="store-opened" (click)="updateShopSlotsStatus(true)" [ngClass]="{'toggled-opened-text': !shopIsOpen}">OPENED</label>
</div>
<div class="label-container">
<label class="store-closed" (click)="updateShopSlotsStatus(false)" [ngClass]="{'toggled-closed-text': !shopIsOpen}">CLOSED</label>
</div>
</div>
效果很好,但现在用户可以向这个切换开关发送垃圾邮件,连续触发多个 http 请求。
我能做些什么来防止这种行为?如果用户向此切换开关发送垃圾邮件,我不想触发 http 请求。也许有办法只触发最后一个?由于我对 RxJs 很陌生,我真的不知道如何解决这个问题。
这里是click事件调用的方法,在组件.ts中:
updateShopSlotsStatus(isOpen: boolean){
this.shopIsOpen = isOpen;
this.apiService.updateShopSlotsStatus(isOpen, this.weekDay).subscribe();
}
还有http请求,在一个服务文件中:
updateShopSlotsStatus(isOpen: boolean, weekDay: string){
const endpoint = this.url + "shipping/" + this.webzineId + "/delivery_slots" + "/";
return this.http.patch(endpoint, {"slots": { [weekDay] : { "enabled": isOpen }}});
}
提前致谢。
【问题讨论】:
-
这能回答你的问题吗? Angular 2+ and debounce(您正在寻找的“东西”称为debouncing。此线程中有许多旧答案,但也有合适的最新答案。)
-
感谢您的建议!我很难弄清楚如何调整其中一些答案来回答我的问题,因为其中许多都是关于表单控件和输入的。
标签: angular typescript rxjs