Attention! This post is older than three years, it may or may not be relevant anymore.

TL;DR

article explains how to avoid slowing down your web app by canceling or aborting previous AJAX requests. It provides a code snippet that assigns the current AJAX request object to a variable and checks if it’s complete before initiating a new request, thus aborting the previous one. The article also includes a solution for ignoring the “abort” error that occurs when a previous request is canceled.

jQuery AJAX is the most common way to deal with AJAX requests these days. You can send as many Ajax requests as you like but there are many cases in which sending too many or frequent Ajax requests is not suitable and best avoided.

For example, let’s say you have developed a certain search functionality that fetches result data using AJAX. The search query string is the value of the input field bound with the Ajax function and is triggered on the keyup event on the input field. In this case, if the user has to enter a query string of let’s say 8 characters, this will mean that there’ll be at least 8 keyup events triggered, which further means that there’ll be at least 8 Ajax calls. This will definitely lead to a slowing of the whole app and is unreasonable as well.

The solution here is to only focus on the query string after the user has finished typing i.e. the last keyup event. So, for this, you have to cancel or abort all the previous Ajax requests.
Let’s say that the input field is as follows:

[PHP]

[/php]

Now, we have to write the code for the Ajax function which will be triggered on the key up event and will also abort the previous Ajax calls if any. So, our code for such a function will be as:

Explanation:

In the above code what’s happening is that we have assigned our $.ajax request to a variable ‘ajaxReq‘. This variable will hold the current Ajax request object and if another Ajax request is called before the previous one is completed it aborts the previous one. It does this by checking the condition set in the beforeSend function which fires before a new Ajax request is initiated.

 The ajaxReq != ‘ToCancelPrevReq’ checks if the ajaxReq variable holds a previous ajax request object and the ajaxReq.readyState < 4 condition checks if that is complete or not. The readyState property can have 5 values depending on the status of the Ajax XMLHttpRequest, these are:

0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready

You can learn more about XMLHttpRequest at:
https://www.w3schools.com/xml/ajax_xmlhttprequest_create.asp

Now, most of the tutorials regarding this topic end here, but aborting the previous request creates another problem if you are catching errors in case of failed Ajax request. You’ll notice that in the error setting function, we have an alert capturing any error that is returned in case of a failed request, its status, and its response text. When we abort a request it’s considered a failed request that will also be captured. But we don’t want to display an alert every time a user types fast and cancels a previous Ajax request in the process, so we add our condition to ignore the ‘abort’ error and just to be safe for any ‘undefined’ error as well.

The condition:

checks this and returns false, if true, without processing the alert function under it.
This is it. This should be a standard and should also be considered a good practice as well.
If you are looking for a reliable team to manage your web development, consider checking out our web development services and pricing page. Feel free to suggest any other good methods that you may have.
Happy Coding 🙂
Currency
USD United States (US) dollar