防止短时间内重复的发布请求

huangapple go评论74阅读模式
英文:

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}}
&lt;button (click) = &quot;value = value+1&quot;&gt;Increase By 1&lt;/button&gt;

TS

const newData = {finalValue: this.value};
this.http.post(this.apiEndpoint, newData).subscribe(res =&gt; 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

英文:

You can use debounce to delay sending the HTTP request if the user clicks again on the button.

Alternatively, you may need throttle in case you want ignore any changes the user makes in between a time interval you set.

huangapple
  • 本文由 发表于 2023年2月27日 19:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定