Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery passing a function call through to another function and order of execution

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 supposed to happen:

  • triggerAnimation is called passing an object and a function
  • triggerAnimation does a dummy animation (to create a pause) then raises an alert and triggers a callback function which executes the function that was passed through.
  • the function that was passed through is called raising an alert.

Based on the above, I'd expect the alert A to appear before alert B but that is not the case. What happens is that (it seems) alert B is called as soon as triggerAnimation() is called. Why is that? How can I achieve that behavior?

like image 818
DA. Avatar asked Nov 12 '09 23:11

DA.


People also ask

How do I execute a function after another function?

click(function(){ if (condition == 'true'){ function1(someVariable); function2(someOtherVariable); } else { doThis(someVariable); } });

How should I call 3 functions in order to execute them one after the other?

If we have three synchronous functions, we can execute them asynchronously using the setTimeout function. setTimeout(doSomething, 10); setTimeout(doSomethingElse, 10); setTimeout(doSomethingUsefulThisTime, 10);

How call jQuery function one after another?

To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.

How do you call a function within a function in jQuery?

function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });


1 Answers

You can delay the execution by passing in a function and calling it later.

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");
};
like image 71
eduffy Avatar answered Sep 18 '22 07:09

eduffy