Problem on jquery with asynchronous function

Hi there,

I have a problem with a script, I vaguely feel where the problem comes from, but I don’t really see how to solve it. I put the relevant part of the code:

Code:

$('.bouton').click( function ()
    button = this;
    button.disabled = true;
     
    $.ajax(
        //sans surprise
    )
    .always(function()
        button.disabled = false;
    );

);

It’s not very original, I have a page with a number of buttons, and I want to execute a server-side action in AJAX. I haven’t put the AJAX part for you, which is no surprise and does not pose a problem, the data sent depends on a different data- attribute for each of the buttons that I easily retrieve. So far, no problem, it works.

The problem is how the .always() is handled when the AJAX call returns. If it takes a little time and the user has clicked on another button before the first request is finished (which I don’t want to prohibit), it will be this other button that will be processed during the call to the always() function. Consequently the first button will never be reactivated, while the second button will be reactivated too soon.

Leave a Replay