Den Rolya
Den Rolya

Reputation: 39

AngularJS Resource prevent duplicate requests

Having pagination on the page I've came across the issue, if the user clicks 3 times on the next page button it will send 3 requests to server. What would be the best solution to prevent it?

I'm using AngularJs 1.6.6 with ngResource.

Upvotes: -1

Views: 362

Answers (2)

danday74
danday74

Reputation: 57225

The best solution is to debounce the button click function. When you debounce a function it will ignore subsequent calls to the function for X seconds.

Lodash comes with a throttle and debounce method for this purpose.

If you don't have Lodash available then just go with disabling the button when it is clicked using ng-disabled

Whilst this sorts things client side, it doesn't prevent someone writing some malicious code to hammer your API, so some server-side protection is a good idea if you have time.

Upvotes: 1

Guy Yogev
Guy Yogev

Reputation: 869

disable the button click till the next page is loaded, or denounce the click event.

Upvotes: 2

Related Questions