This is breaking my head, I have 2 functions
1) to get an ID
2) to call a second function with the ID which was retrieved.
Now I need to combine them in one function ,but obviously the 2nd function gets
called before GetSelectedID() has returned
function FillControl() {
var iSelectedID = GetSelectedID();
SetControlValuesWithSelectedID(iSelectedID);
}
function GetSelectedID() {
$.ajax({
url: '123.ashx',
type: 'POST',
contentType: 'application/json',
success: function (data1) {
return data1;
}
});
}
Please help to get the second function to be called only once first function has been called.
Your
GetSelectedID()function is asynchronous, as it performs an AJAX request. It will return immediately, butiSelectedIDwill only be available some time later.You can pass a callback function to
GetSelectedID(), call that function from the success callback passed to$.ajax(), and relocate the call toSetControlValuesWithSelectedIDin that function:Then: