简体   繁体   English

自定义函数中的回调函数

[英]callback function in custom function

is there a way to call a custom function after another completes. 有没有办法在另一个完成后调用自定义函数。 Lots of jquery includes a way to do a call back, like when an animation completes you can just give it another function as a last parameter. 很多jquery都包含一种回调方式,比如动画完成时你可以将另一个函数作为最后一个参数。 But is there anyway to string together function1(params, function1(){alert('2');}) or something? 但是无论如何将function1(params, function1(){alert('2');})或其他东西串在一起? I honestly have no idea how it would look. 老实说,我不知道它会是什么样子。

Thank you 谢谢

You can create a function pointer with something like: 您可以使用以下内容创建函数指针:

var yourCallback = function() {
    alert('2');
}

You can think of it as an object containing the function itself (not its return value). 您可以将其视为包含函数本身的对象(而不是其返回值)。 Since it is an object, you can pass it around to other functions. 由于它是一个对象,您可以将其传递给其他函数。

Then pass that to the function you are calling as an argument (do not invoke it with () , as that would pass its return value): 然后将它传递给您作为参数调用的函数(不要使用()调用它,因为它将传递其返回值):

function1(params, yourCallback);

Finally, in function1, invoke your callback when you need it: 最后,在function1中,在需要时调用回调:

function function1(args, callback) {
    //Some code
    if (callback) {
        callback();
    }
}

Technically the pattern you're asking for is: 从技术上讲,你要求的模式是:

function myFunctionNeedsACallback(param1, callback) {
    //do stuff with param1
    if(typeof callback === "function"){
        callback.call(yourThisParam, anyOtherParams);
    }
}

While this is a useful pattern... overuse will lead to ugly, untestable, and unmaintainable code. 虽然这是一个有用的模式...过度使用将导致丑陋,不可测试和不可维护的代码。 Use with discretion. 谨慎使用。

Your example would work just fine: 你的例子可以正常工作:

myFunctionNeedsACallback(param, function() { alert('2'); });

//will alert 2 after running the rest of the contents of the function

EDIT 编辑

To address thomas's situation: 为了解决托马斯的情况:

function ajaxCall1(){
    return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
}

function ajaxCall2(){
    return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
}

function ajaxCall3(){
    return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
}

$.when(ajaxCall1(), ajaxCall2(), ajaxCall3()).then(function(results1, results2, results3) {
    //do stuff with all 3 results without "nesting" things
});
function doSomething(arg1, callback){
    // bla bla bla
    callback();
}

You'd structure your function something like this: 你的结构是这样的:

function one(x, y, z, callback) {
   // do operations here

   callback();
}

Then, since functions are objects you could pass in a function through the callback parameter, IE: 然后,由于函数是对象,您可以通过回调参数IE传递函数:

one(1, 2, 3, function(){alert('hello');});

Or 要么

var func = function() { alert('hello'); };
one(1, 2, 3, func);

Or 要么

function func() {
  alert('hello');
}

one(1, 2, 3, func);

While callbacks are convenient, overuse can lead to ugly and unmaintainable code. 虽然回调很方便,但过度使用会导致丑陋且无法维护的代码。 Besides standard callbacks shown in other answers, you might want to look at other patterns like deferred/promises and AOP. 除了其他答案中显示的标准回调之外,您可能还想查看其他模式,如延迟/承诺和AOP。

In jQuery, promises are implemented with when() and then() . 在jQuery中,promises是用when()和then()实现的

requirejs is also a good startpoint. requirejs也是一个很好的起点。

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

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