1

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!

0

2 Answers 2

4

You are passing the invocation of a function, not a function that should be called at a later point. If you want the latter, do this:

save (
   (function(a,b){
      return function() {
        return updateFilters(a,b)
      }
   })(a,b)
);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! I'm trying to understand the sequence of calls, which function is called when. Can you help?
Put alerts inside of each function with unique values and when you see those alerts you'll know which functions are called.
that's my issue. alert from the second return is never called.
i mean putting alert after return function (){alert ('r1');... never appears
You are now referencing code that no one is able to see. Please post the modified alert code you have.
|
1

Regarding your update, you never invoke the callback. It's the same as doing

x; // does nothing

You want to call the callback:

cb(); // invoke the function

1 Comment

Thanks! I did not realize importance of parentheses!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.