$(document).ready(function () {
$("#anca").click(function (e) {
viewModel.CurrentVisit.GetById(StartValidation);
//Below statement executes before startValidation callback function //is executed
var x= true;
});
});
正如,你可以看到我在这里调用函数GetById和StartValidation是回调函数
self.GetById = function (SuccessCallback) {
var UId= self.Id();
UId = UId.replace('emp/', '');
$.ajax({
type: "GET",
url: "/api/v1/emp/" + UId,
traditional: true,
success: function (data) {
SuccessCallback(UserId);
},
error: function (data) {
debugger;
}
});
}
回调函数定义为
function StartValidation(UserId) {
if (viewModel.CurrentUser.Id() != UserId) {
alert('Fail');
return false;
}
}
基本上回调函数稍后执行,语句var x = true;首先执行。我理解它的anasync调用所以如何等待回调函数完成beore语句var x = true;被执行了吗?