Skip to content
the poor coder

how to prevent ajax from call twice

Poor Man
Published
Reading time
2 min read
On this page

Preventing AJAX from Calls Twice#

As a web developer, I have had numerous encounters with AJAX calls being executed twice. This can be quite frustrating especially when dealing with complex projects that require multiple AJAX requests. I have tried several ways to prevent this issue over time, and below are my top three approaches:

This approach involves disabling the button/link that triggers the AJAX event immediately after it is clicked. Here is an example:

Text

    $('button#myButton').on('click', function(e) {
        e.preventDefault();
        $(this).prop('disabled', true); // disables the button
        $.ajax({
            url: 'example.php',
            type: 'POST',
            data: {name: 'John'},
            success: function(response) {
                // handle success
            },
            complete: function() {
                $('button#myButton').prop('disabled', false); // enables the button
            }
        });
    });

The 'disabled' attribute is added to the button/link, making it unclickable until the AJAX request completes. Once the request is done, the 'disabled' attribute is removed, and the button/link becomes clickable again. This way, even if the user clicks the button/link more than once, only the first click will trigger the AJAX request.

2. Using a Flag to Check AJAX Status#

Another way to prevent AJAX from calling twice is by using a flag to check its status. Here is an example:

Text

    var ajaxInProgress = false;

    $('button#myButton').on('click', function(e) {
        e.preventDefault();
        if(!ajaxInProgress) {
            ajaxInProgress = true;
            $.ajax({
                url: 'example.php',
                type: 'POST',
                data: {name: 'John'},
                success: function(response) {
                    // handle success
                },
                complete: function() {
                    ajaxInProgress = false;
                }
            });
        }
    });

The variable 'ajaxInProgress' is set to 'false' by default. When the button/link is clicked, it checks if 'ajaxInProgress' is 'false'. If it is, the AJAX request is executed, and 'ajaxInProgress' is set to 'true'. If the user clicks the button/link again while the AJAX request is still in progress, the request is ignored because 'ajaxInProgress' is still 'true'. Once the request completes, 'ajaxInProgress' is set back to 'false', making the button/link clickable again.

3. Using jQuery's one() Method#

The jQuery one() method can also be used to prevent AJAX calls from executing twice. Here is an example:

Text

    $('button#myButton').one('click', function(e) {
        e.preventDefault();
        $.ajax({
            url: 'example.php',
            type: 'POST',
            data: {name: 'John'},
            success: function(response) {
                // handle success
            }
        });
    });

The jQuery one() method attaches a function to the button/link that triggers the AJAX event. This function executes only once, making it impossible for the user to click the button/link more than once. Once the AJAX request is completed, the function is detached from the button/link, making it clickable again.

Conclusion#

AJAX calls being executed twice can cause a lot of issues, but fortunately, there are several ways to prevent this from happening. The above approaches are just a few of the many ways to handle the issue, and developers should choose the one that best suits their needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC