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
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 to share
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 to share
Yes it is possible. You can use . AjaxStop () or . AjaxComplete () or . ajaxSuccess ()
-1
source to share