How to avoid multiple AJAX calls?

henrywright

I'm submitting a form via AJAX using the code below:

$( 'form' ).submit(function(e) {

    $.ajax({
        type: 'POST',
        url: ajax_url,
        dataType: 'json',
        data: {
            'action': 'my_action',
            'str': $( 'form' ).serialize()
        },
        success: function( data ) {
            // Do something here.
        },
        error: function( data ) {
            // Do something here.
        }
    });
    return false;
});

Background

My PHP handler carries out various tasks and then sends back a response. I can then do something with data in either of the success or error functions.

My problem

When a user double clicks on the form's submit button, two AJAX calls take place which causes the code inside my PHP handler to execute twice.

My question

How can I avoid my code being executed twice if a user double clicks on submit?

Raidri supports Monica

Disable the submit button on the first click and re-enable it, when the AJAX call comes back.

For example:

$( 'form' ).submit(function(e) {
    var $form = $(this);
    $form.find('submit').attr('disabled', true);
    $.ajax({
        type: 'POST',
        url: ajax_url,
        dataType: 'json',
        data: {
            'action': 'my_action',
            'str': $( 'form' ).serialize()
        },
        complete: function() {
            $form.find('submit').removeAttr('disabled');
        },
        success: function( data ) {
            // Do something here.
        },
        error: function( data ) {
            // Do something here.
        }
    });
    return false;
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid multiple AJAX calls?

From Dev

How To Reduce Multiple Ajax Calls

From Dev

AJAX multiple ajax calls

From Dev

How to avoid ERR_INSUFFICIENT_RESOURCES on AJAX calls

From Dev

How to render content with multiple AJAX calls with Polymer?

From Dev

How to call multiple ajax calls on a view?

From Dev

How to avoid or stop multiple ajax request

From Dev

How to avoid or stop multiple ajax request

From Dev

Why Singleton in python calls __init__ multiple times and how to avoid it?

From Dev

How to avoid multiple 'libFoo.so' open() calls at application launch?

From Dev

Restrictions of Multiple AJAX Calls?

From Dev

Multiple Ajax Calls

From Dev

Waiting for multiple Ajax calls

From Dev

Multiple AJAX calls optimisations

From Dev

How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls?

From Dev

How can I have multiple ajax calls in one route?

From Dev

how to pass multiple ajax calls in a single django view

From Dev

How to set delay for multiple ajax calls and bind a stop button to them?

From Dev

How to avoid multiple ajax call from the Jquery Autocomplete ? Using Cache

From Dev

How to avoid nesting method calls?

From Dev

How to avoid nested forEach calls?

From Dev

How to avoid concurrent webservice calls?

From Dev

How to avoid out of memory error in a browser due to too many ajax calls

From Dev

Avoid multiple function calls in C#

From Dev

What exactly does it mean to avoid multiple calls?

From Dev

What exactly does it mean to avoid multiple calls?

From Dev

Avoid multiple WMI calls to same computer?

From Dev

when multiple ajax calls done

From Dev

Multiple Ajax Calls, One Callback

Related Related

HotTag

Archive