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.
You can use .ajaxComplete()
a request method ajax
that registers a handler to be called when the ajax
request is complete .
More details here.
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)
{
}
});
});
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
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 :-)
try using setTimeout.
setTimeout(function(){
$('#mymodal').modal('show');
},4000);
Yes it is possible. You can use . AjaxStop () or . AjaxComplete () or . ajaxSuccess ()