0

We have a search bar and pagination on the page, using jquery to make ajax request As one start searching for particular record many ajax request is being fired , though we added ajax abort to the previous request but in rails query is still happening for all,

How to stop the rails processing when previous ajax request is aborted

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59

1 Answers1

3

If the Rails server has received the AJAX request, .abort will not stop the server from executing it. This stackoverflow answer describes how you could use readyState to check if the last request was completed or not.

However, I believe what you're looking for is a debounce function. A debounce function limits the rate at which a function gets fired. You could use that to make sure that the search bar fires a backend request only after certain seconds have passed since it last received an input. That would greatly reduce the number of queries that get sent to the Rails server.

Anuj Khandelwal
  • 1,214
  • 1
  • 8
  • 16
  • You could also use a promise to ensure that the call is not repeated until the server responds. – max Jan 02 '20 at 15:34