Can anyone tell me how to create functions with callback? I have created this function but it is not working.
function PostsCategoriasListar(table, select, callback) {
if (table) {
$.ajax({
url: controller_dashboard,
type: 'POST',
dataType: 'json',
data: {acao: 'PostsCategoriasListar', Listar: 'table'},
success: function(response) {
if (response.status === 'LogOff') {
window.setTimeout(function() {
window.location.reload();
}, 10000);
}
$element.find('table tbody').html(response.table);
ExecuteDataTable(0, maximo, 1);
},
error: function(error) {
console.log(error);
}
});
}
if (select) {
$.ajax({
url: controller_dashboard,
type: 'POST',
dataType: 'json',
data: {acao: 'PostsCategoriasListar', Listar: 'select'},
success: function(response) {
if (response.status === 'LogOff') {
window.setTimeout(function() {
window.location.reload();
}, 10000);
}
$element.find('form select').html(response.select);
},
error: function(error) {
console.log(error);
}
});
}
//EU QUERO QUE O CALLBACK SO EXECULTE DEPOIS QUE TODO CODIGO QUE ESTIVER NOS IF'S ACIMA TENHAN TERMINADO, ELE SEMPRE EXECULTA ANTES ONDE ESTOU ERRANDO?
if (typeof callback === 'function')
callback();
}
PS: I did not put the ajax requests because it would make it very large, because they are working only if I need them to execute first and after the callback is executed