我正在尝试编写一个函数,该函数将执行某些操作,然后在完成后绑定(bind)回调,我想在初始化 js 函数时指定回调...
function hello(arg1,arg2,callback){
alert(arg1 + arg2);
callback;
}
hello('hello','world',function callback(){
alert('hey 2);
});
很抱歉问了一个平庸的问题,我正在尝试了解如何将回调函数传递给函数。
最佳答案
您需要像调用其他函数一样调用该函数:
function hello(arg1,arg2,callback){
alert(arg1 + arg2);
callback();
}
hello('hello','world',function callback(){
alert('hey 2);
});
请注意,在 JavaScript 中,有更好的方法来执行该回调,例如 .apply()或call()但只有当您计划在回调中使用 this 关键字时才需要这样做。
关于javascript - 带回调的 jQuery 简单函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126926/