0

I want to unbind click event of button until ajax call completes.

It unbinds click but I am not able to make it rebind click.

Below is my Ajax Call:-

   $("#btnsearch").click(function (event) {        
            $('#btnsearch').text('Wait...').unbind('click');
            $.ajax({
                url: "//api/Ulyx/OrderDetails",
                type: "GET",
                dataType: "json",
                beforeSend: function () {               
                },
                async: false,           
                },
                crossDomain: true,
                complete: function (data) {                 
                    $('#btnsearch').text('Search').bind('click');                  
                }
            });
1
  • You need to specify the event handler as well. Commented Jan 16, 2018 at 9:38

1 Answer 1

1

Rather than binding/unbinding events, you could simply store a state variable to say that an AJAX request is in progress to disallow the sending of another one:

$("#btnsearch").click(function(event) {
  if (!$(this).data('ajax-in-progress')) {
    $(this).text('Wait...').data('ajax-in-progress', true);

    $.ajax({
      url: "//api/Ulyx/OrderDetails",
      type: "GET",
      dataType: "json",
      crossDomain: true,
      complete: function(data) {
        $('#btnsearch').text('Search').data('ajax-in-progress', false);
      }
    });
  }
});

Also note that I removed the async: false setting as it's very bad practice, and I fixed the syntax issues with the call, which I presume were just caused when copying the code to the question.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks @Rory, will this function stop clicking the button if ajax didn't complete?
No, as the complete is always executed, regardless if the AJAX request was a success or not.
but i need to stop clicking on the same button untill ajax completes, pls modify your answer....
That's what my answer does. If the #btnsearch element is a button you could just disable it.
i used disabled as well but i dont why its not working may be due to asyn false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.