JQuery passes a function call to another function and execution order

I have this javascript:

triggerAnimation(listItem,toggleToggleRadioListItem(listItem));


function triggerAnimation(listItem,passThruFunction){
    listItem.find(".inlineLoading").show();
// pause and then call the toggle function
    $("body").animate({opacity: 1}, 1000, 
    function(){
        alert("a");
    passThruFunction;
    }
    );  
}

function toggleToggleRadioListItem(listItem) {
    alert("b");
};

What is going to happen:

  • triggerAnimation is called passing an object and function
  • triggerAnimation makes a dummy animation (to pause it), then triggers a warning and launches a callback function that executes the passed function.
  • the function that was passed is called raising the alert.

Based on the foregoing, I expect warning A to appear before signal B, but this is not the case. It happens that (it seems) warning B is triggered as soon as triggerAnimation () is called. Why is this? How can I achieve this behavior?

+5
source share
4 answers

, .

triggerAnimation(listItem, function () {
   toggleToggleRadioListItem(listItem)
});


function triggerAnimation(listItem,passThruFunction){
    listItem.find(".inlineLoading").show();
// pause and then call the toggle function
    $("body").animate({opacity: 1}, 1000, 
    function(){
        alert("a");
        passThruFunction();
    }
    );  
}

function toggleToggleRadioListItem(listItem) {
    alert("b");
};
+6

, , passThruFunction . Javascript , , , , ( "b" ). [ ], .

+2

toggleToggleRadioListItem(listItem) triggerAnimation(listItem,toggleToggleRadioListItem(listItem));, toggleToggleRadioListItem(listItem).

+1

edpidy's answer is the simplest thing that can work, and will work. If you need something more hardcore, quietly reading a partial application can give you another way to add to your toolbox.

+1
source

All Articles