简体   繁体   English

如何在具有多个回调的函数中获取最后一个回调?

[英]How to get the last callback in a function with several callbacks?

I have a showMain() function with a lot of jQuery .show("slow", callback) methods, and each of these callbacks are triggered only when the corresponding slow "animation" is complete. 我有一个带大量jQuery .show("slow", callback)方法的showMain()函数,并且仅当相应的慢速“动画”完成时才触发每个回调。 How to write a showMain(callback) function, whose callback will be triggered only once and after all animations are complete? 如何编写showMain(callback)函数,该回调函数仅在所有动画完成后才触发一次回调?

Theoretically, imagine I have this set of functions 从理论上讲,假设我有这套功能

 function foo1(callback){ //do something callback(); } function foo2(callback){ //do something callback(); } function foo(callback){ foo1(callback); foo2(callback); } foo(function(){ alert("Hello"); }); 

How to write a function that will call its callback only once , and the last of them to be executed? 如何编写一个仅调用一次回调, 最后执行一次的函数?

If I write this, the callback will be triggered twice. 如果我写这个,回调将被触发两次。

how about maintaining a counter if you know how many times you are calling show, 如果知道要打多少次节目,如何维护一个计数器呢?

var callback = function() {....} // the function you want to execute when all done
var itemsToShow = 4; // just an example
var slowCallback = function(){
    itemsToShow--;
    if(itemsToShow===0) {
        callback();
    }
};
$(...).show('slow', slowCallback);
$(...).show('slow', slowCallback);
$(...).show('slow', slowCallback);
$(...).show('slow', slowCallback);

Note : It will work only if you know How many times you will call .show otherwise initialize the counter to 0 and increment with each show call 注意 :仅当您知道要调用多少次时,它才会起作用.show否则将计数器初始化为0并在每次show调用时递增

if you want to work with callbacks then you will need a control var. 如果您想使用回调,则需要一个控制变量。 I would solve your problem doing something like: 我将通过以下方式解决您的问题:

var i = 2 // number of your "shows" methods.
function foo1(callback){
  //do something
  x = x-1;
  if(x<=0)callback();
}

function foo2(callback){
  //do something
  x = x-1;
  if(x<=0)callback();
}

function foo(callback){
  foo1(callback);
  foo2(callback);
}

Your maybe you consider using Promises and Promises.all should solve your problem too. 您也许考虑使用PromisesPromises.all应该解决您的问题。

You could bypass the control variable issue by converting them to Promises. 您可以通过将它们转换为Promises来绕过控制变量问题。 See this fiddle: https://jsfiddle.net/kqso0kkb/1/ 看到这个小提琴: https : //jsfiddle.net/kqso0kkb/1/

$(document).ready(function() {
  console.clear();

  Promise.all([one(), two()]).then(() => {
    console.log('all done');
  })

  function one() {
    return new Promise((resolve, reject) => {
      $('#div').show("slow", resolve);
    }).then(() => console.log('done 1'));
  }

  function two() {
    return new Promise((resolve, reject) => {
      $('#div').show("slow", resolve);
    }).then(() => console.log('done 2'));
  }
});

Instead of console.log you can put whatever callback you like. 您可以放任何您喜欢的回调而不是console.log This way you can load up an array of any size with promises, and .all will take care of executing your function when they're all finished 这样,您可以使用promise加载任何大小的数组,并且.all将在完成所有操作后执行您的函数

That's how I solved the issue: 这就是我解决问题的方法:

 function foo(callback){ $("#div1").show("slow"); $("#div2").show("fast"); if (typeof callback === 'function'){ $("#div1, #div2").promise().done(callback); } } 
 #div1, #div2{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1"> Text1 </div> <div id="div2"> Text2 </div> <input type="button" value="show" onClick="foo(function(){alert('Show Once')})" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM