I have a angular application where there is a toolbar on top. There are some button on the toolbar which is working fine except when the user clicks it twice. For example, when the following button is clicked twice, it saves the data twice on the server.
<a class="nav-link" (click)="buttonClicked()" href="#">
<i class="fas fa-save" title="History"></i>
</a>
How can I stop this from happening.. May be I need to disable the button for few milliseconds in order to avoid accident clicking… But not sure how can I achieve this…
Thanks in advance.
2
Answers
Disable the button after the first click, until the API call is done or errors out. I use
pointer-events: none;
to prevent clicks, andopacity: 0.5;
to make the button seem disabled. But you can use this to solve your issue.CSS:
HTML:
TS:
You can use rxjs and the throttleTime operator. It will skip the n requests sended in between x amout of time.
Here is an example https://stackblitz.com/run?devtoolsheight=50&file=index.ts