UPDATE3 and FINAL: SOLVED thanks to Evan and meder!
UPDATE2: I should clarify, I need function updateFilters (a,b)
to be called, not created. The function exists elsewhere. Sorry for confusion.
The code below does not work as expected - udpateFilters(a,b)
is being called before for
loop ends. Changing async to false
solves the problem, but this does not seem right. Moreover updateFilters() is called regardless of ajax success.
What did I do wrong?
save (updateFilters(a,b));
function save (cb) {
$.ajax ({
url:'save.php',
data:somedata,
async:true,
success: function (response) {
for (var i in response) {}
if (typeof cb!='undefined') cb;
});
}
function updateFilters (a,b) {
//do some stuff here
}
ANSWER: So as meder suggested I change the parameter. This is 2/3 of the answer!
save (
(function (a,b) {
return function () {
return updateFilters(a,b);
}
})(a,b)
);
Now change the following:
if (typeof cb!='undefined') cb (); //brackets are important!
And good to go!