英文:
Prevent repeat post calls in short succession
问题
我有一个情况,用户可以随意多次更新一个值,但我想避免用户不断更改该值以发送垃圾请求到服务器。
是否有方法可以实现这一点?
使用情况示例
HTML
您的新值为{{value}}
<button (click)="value = value+1">增加1</button>
TS
const newData = {finalValue: this.value};
this.http.post(this.apiEndpoint, newData).subscribe(res => console.log(res));
我的一个想法是使用 Subject 来实现这一点,但如果有更简单的方法,学习起来会更好。
英文:
I've got a case where user can update a value as many times as they like, however I want to avoid spamming the server until user basically stops changing the value.
Is there a way I can accomplish this?
Use Case Example
HTML
Your New Value is {{value}}
<button (click) = "value = value+1">Increase By 1</button>
TS
const newData = {finalValue: this.value};
this.http.post(this.apiEndpoint, newData).subscribe(res => console.log(res));
My one thought is to use Subject to achieve this, but if there is a simpler way would be good to learn.
答案1
得分: 1
你可以使用debounce
来延迟发送HTTP请求,如果用户再次点击按钮。
或者,如果您希望在设置的时间间隔内忽略用户所做的任何更改,可以使用throttle
。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论