自定义函数中的回调函数 [英] callback function in custom function
问题描述
有没有办法在另一个完成后调用自定义函数。很多jquery都包含一种回调方式,比如动画完成时你可以将另一个函数作为最后一个参数。但无论如何要将 function1(params,function1(){alert('2');})
或其他东西串在一起?老实说,我不知道它会是什么样子。
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. But is there anyway to string together function1(params, function1(){alert('2');})
or something? I honestly have no idea how it would look.
谢谢
推荐答案
你可以用以下内容创建一个函数指针:
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.
然后将它传递给你作为参数调用的函数(不要用<$调用它) c $ c>(),因为它会传递其返回值):
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);
最后,在function1中,在需要时调用你的回调:
Finally, in function1, invoke your callback when you need it:
function function1(args, callback) {
//Some code
if (callback) {
callback();
}
}
这篇关于自定义函数中的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!