jQuery将函数调用传递给另一个函数和执行顺序

时间:2022-12-05 14:27:47

I have this javascript:

我有这个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被称为传递一个对象和一个函数
  • 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.
  • triggerAnimation执行一个虚拟动画(创建一个暂停),然后触发一个警报并触发一个回调函数,该函数执行所传递的函数。
  • 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?

基于以上内容,我希望警报A出现在警报B之前,但事实并非如此。发生的情况是(看起来)在调用triggerAnimation()时就会调用alert B。这是为什么呢?我怎样才能达到那种行为呢?

4 个解决方案

#1


6  

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");
};

#2


2  

Well, you could pass the function reference and an array of parameters to trigger animation instead, which is then passed to passThruFunction on execution. In Javascript, what you pass to a function will always be evaluated before the function code is actually executed, thats why you get your alert("b") first. Thats called busy evaluation [of function parameters], btw.

你可以传递函数引用和参数数组来触发动画,然后在执行时传递给passThruFunction。在Javascript中,在函数代码实际执行之前,您传递给函数的内容总是要被评估的,这就是为什么您首先要得到警告(“b”)。顺便说一句,这叫做“函数参数的繁忙评估”。

#3


1  

Because you call the toggleToggleRadioListItem(listItem) when you call triggerAnimation(listItem,toggleToggleRadioListItem(listItem));, then executes toggleToggleRadioListItem(listItem) first.

因为当您调用triggerAnimation(listItem)时,您调用了toggletoggleradiolitem (listItem);然后首先执行toggleToggleRadioListItem(listItem)。

#4


1  

eduffy's answer is the simplest possible thing that could work, and will work a treat. If you want something more hardcore, a quiet read of partial application might give you another technique to add to your toolbelt.

eduffy的回答是最简单的可能的事情,它能起作用,也会起作用。如果你想要更硬的东西,一个安静的部分应用程序的阅读可能会给你另一个添加到你的工具带的技术。

#1


6  

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");
};

#2


2  

Well, you could pass the function reference and an array of parameters to trigger animation instead, which is then passed to passThruFunction on execution. In Javascript, what you pass to a function will always be evaluated before the function code is actually executed, thats why you get your alert("b") first. Thats called busy evaluation [of function parameters], btw.

你可以传递函数引用和参数数组来触发动画,然后在执行时传递给passThruFunction。在Javascript中,在函数代码实际执行之前,您传递给函数的内容总是要被评估的,这就是为什么您首先要得到警告(“b”)。顺便说一句,这叫做“函数参数的繁忙评估”。

#3


1  

Because you call the toggleToggleRadioListItem(listItem) when you call triggerAnimation(listItem,toggleToggleRadioListItem(listItem));, then executes toggleToggleRadioListItem(listItem) first.

因为当您调用triggerAnimation(listItem)时,您调用了toggletoggleradiolitem (listItem);然后首先执行toggleToggleRadioListItem(listItem)。

#4


1  

eduffy's answer is the simplest possible thing that could work, and will work a treat. If you want something more hardcore, a quiet read of partial application might give you another technique to add to your toolbelt.

eduffy的回答是最简单的可能的事情,它能起作用,也会起作用。如果你想要更硬的东西,一个安静的部分应用程序的阅读可能会给你另一个添加到你的工具带的技术。