jQuery Jquery在自定义函数中调用回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7866787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery calling a callback function in a custom function
提问by Mr. Sam
I have a custom function and I want to add a callback function to it, i looked through the other questions but I couldn't really find the answer for my problem. So basically it looks like this:
我有一个自定义函数,我想向它添加一个回调函数,我查看了其他问题,但我无法真正找到我的问题的答案。所以基本上它看起来像这样:
function myfunction() {
// something
}
And I have a different function which I want to trigger as a callback, eventually i want to call myfunction like this:
我有一个不同的函数,我想作为回调触发,最终我想像这样调用 myfunction:
myfunction(callback());
So can anyone please tell my the way to do this?
那么任何人都可以告诉我这样做的方法吗?
回答by jfriend00
You would call it like this (passing the callback function as an argument):
您可以这样调用它(将回调函数作为参数传递):
myfunction(myCallbackFn);
Notice that there are no parentheses after the function name when passing a reference to the function as an argument. This does not invoke the function, but just passes a reference to it which can then be called later from within your custom function.
请注意,将函数的引用作为参数传递时,函数名后没有括号。这不会调用该函数,而只是传递对它的引用,然后可以在您的自定义函数中调用该引用。
You would define your function and then call the callback function in it like this:
你可以定义你的函数,然后像这样在其中调用回调函数:
function myfunction(cb) {
cb(arg1, arg2);
}
Note, there are multiple methods of actually calling the callback function from within your custom function. If you just need to call it normally and don't care about the this
argument, you can just use:
请注意,有多种方法可以从自定义函数中实际调用回调函数。如果您只需要正常调用它而不关心this
参数,则可以使用:
cb();
If you want to set the this
argument to something, you can use this:
如果要将this
参数设置为某些内容,可以使用以下命令:
cb.call(thisArg, arg1, arg2);
If you want to pass along your current arguments or pass an array of arguments and set the this
argument, you would use this:
如果要传递当前参数或传递参数数组并设置this
参数,可以使用以下命令:
cb.apply(thisArg, argArray);
References on .call()
and .apply()
.
Your current code has a javascript error in this area:
您当前的代码在这方面有一个 javascript 错误:
test(rand_gyumolcs, link, callback){
callback();
};
This is not legal javascript. I don't know what you're trying to do, but this looks like half of a function declaration and half of a function call. If you just want to call the test function, you would just use this:
这不是合法的 javascript。我不知道您要做什么,但这看起来像是函数声明的一半和函数调用的一半。如果你只想调用测试函数,你可以使用这个:
test(rand_gyumolcs, link, callback);
Inside of test, if you want the callback function to be called after the animation is complete, then you would have to hook the completion function of the animation and call the callback then.
在测试内部,如果您希望在动画完成后调用回调函数,则必须挂钩动画的完成函数并调用回调。
回答by Remco Nonhebel
Here's an example with a callback with a return value that's also taking input parameters:
这是一个带有返回值的回调示例,该回调也接受输入参数:
function test2(arg1, arg2, callback) {
var result = callback(arg1, arg2);
alert('arg1=' + arg1 + ', arg2=' + arg2 + ', result=' + result);
}
test2(1, 2, function(a, b) { return a + b; });
回答by Keith.Abramo
function myfunction(callback) {
// something
callback();
}
myfunction(callback);
notice I am not invoking the callback but just passing it as a function reference to my function. I then invoke it IN the function at the end or wherever you want to do it.
请注意,我没有调用回调,而是将其作为函数引用传递给我的函数。然后我在最后的函数中或您想要执行的任何地方调用它。