I was reading stackoverlow and found that there many questions related to this tag, i want to run two piece of codes simultaneously
using the $when
my code looks like this, but not sure how can i rewrite it, please guide
$(document).ready(function() {
$(document).on('click', '#Button',function (e) {
e.preventDefault();
var isExcel = document.getElementById('Excel').checked ? 1 : 0;
$.ajax({
url: "page1",
cache: false,
data : $('#form').serialize(),
method: "post",
success: function(response){
$('.mytable').show();
$('.mytable').html(response);
}
}).done(function(data) {
$("#datatables").DataTable({
"bFilter": true,
"serverSide": true,
"deferRender":true,
"processing": true,
"ajax": {
"url" : "/data.cfm",
"type" : 'post'
}
}
});
});
if(isExcel) {
$.ajax({ // 2nd call
url: "/excel.cfm",
cache: false,
data : $('#form').serialize(),
method: "post",
success: function(response){
alert('done');
}
});
}
});
});
please guide how can i make it work using when, because the ajax call with datatables will bring the results back, the excel might take a while
I want that when i click the hit button, it goes to the ajax call first which brings the datatables and start showing it. If the excel checkbox is checked, it calls another jquery ajax which generates the file from a different code in excel, but the datatables should be visible and in running state.
If you want the Excel part to run after the datatables returns, you should add the Excel request in the done callback of the datables call:
$(document).ready(function() {
$(document).on('click', '#Button',function (e) {
e.preventDefault();
var isExcel = document.getElementById('Excel').checked ? 1 : 0;
$.ajax({
url: "page1",
cache: false,
data : $('#form').serialize(),
method: "post",
success: function(response){
$('.mytable').show();
$('.mytable').html(response);
}
}).done(function(data) {
$("#datatables").DataTable({
"bFilter": true,
"serverSide": true,
"deferRender":true,
"processing": true,
"ajax": {
"url" : "/data.cfm",
"type" : 'post'
}
}
});
if(isExcel) { // moved here
$.ajax({ // 2nd call
url: "/excel.cfm",
cache: false,
data : $('#form').serialize(),
method: "post",
success: function(response){
alert('done');
}
});
}
});
// removed excel request from here
});
});