angularjsangular-resourcengresource

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.


Solution

  • 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.