Is it possible in a click function in jQuery to wait for the ajax to complete and then do some code?

I want to show modal after ajax completes, here is my code:

$('.mymodalbtn').on('click', function (e) {
    modalWindow = $(this).attr('data-target');
    // alert(modalWindow);
    $(document).ajaxStop(function () {
         //0 === $.active
        $(modalWindow).modal('show');

    });

});

      

It can be done? Now modal show after every ajax finishes.

Thank.

+3


source to share


6 answers


You can use .ajaxComplete()

a request method ajax

that registers a handler to be called when the ajax

request is complete .



More details here.

+2


source


Use full event when your ajax call is complete.



$('.mymodalbtn').on('click', function (e) {
    modalWindow = $(this).attr('data-target');

    $.ajax({
        type: 'GET',
        url: 'your_url',
        success: function (responseData, textStatus) {

        },
        complete: function (textStatus) {
            $(modalWindow).modal('show');
        },
        error: function (responseData)
        {

        }
    });

});

      

+1


source


I'm not sure how you are making the ajax call, but I was able to do it with super-global post

$ _ post (phpfile.php, $_post(phpfile.php, {variables to send}, function)

you can use data as a parameter to a function and it will be something that PHP echoes, including error messages

0


source


Thanks for the inspiration, finally I do it like this.

global variable:

window.modal = "";

      

click function:

$('.mymodalbtn').on('click', function (e) {
    modalWindow = $(this).attr('data-target');     
    window.modal = modalWindow;   

});

      

and ajax complete:

$(document).ajaxComplete(function (event, request, settings) {
       if(window.modal != ""){
           $(window.modal).modal('show');
           window.modal = "";
       }    
});

      

Hope everything is in order :-)

0


source


try using setTimeout.

setTimeout(function(){
   $('#mymodal').modal('show');
},4000);

      

-1


source


Yes it is possible. You can use . AjaxStop () or . AjaxComplete () or . ajaxSuccess ()

-1


source







All Articles