admin管理员组文章数量:1332352
How can I create a function, that will call another function, and when it pletes, fire another callback function?
so existing functions are:
function f1(..) {..}
function myCallback() {...}
Now is it possible to make f1 fire and finish, THEN run myCallback()?
How can I create a function, that will call another function, and when it pletes, fire another callback function?
so existing functions are:
function f1(..) {..}
function myCallback() {...}
Now is it possible to make f1 fire and finish, THEN run myCallback()?
Share Improve this question asked Sep 30, 2010 at 16:51 BlankmanBlankman 267k332 gold badges795 silver badges1.2k bronze badges7 Answers
Reset to default 15Provide a function reference as a parameter to the function you're calling.
function f1(fn) {
// ...
if (typeof fn === 'function') {
fn();
}
}
// can be a defined function name or a variable holding a reference to a function
f1(myCallback);
f1();
myCallback();
… unless f1
is asynchronous, in which case f1
would have to be edited to accept a callback and run it when it is finished. Since there are multiple things that could make a function asynchronous, it isn't possible to give a simple "…and this is how" answer without a lot more detail.
function f1(param1, callback){
// Do Work
callback();
}
function myCallback(){
// Do Callback Work
}
And then call f1 like:
f1(parameterValue, myCallback);
if f1 is just a simple javascript-function it runs synchonous, so you just have to call myCallback after it/at the end of f1. if if does some crazy ajax-stuff (with jquery in your case), there you can set a callback on these ajax-things.
You could create a facility to let you bind "onfinish" functions to any function:
function bindFollowup(f, followup) {
return function() {
f.apply(this, arguments);
followup && followup();
};
}
Then you can just define f1
and then write:
f1 = bindFollowup(f1, myCallback);
function f1(myCallBack)
{
// f1 activities
myCallBack();
}
lincolnk provided a great answer. It can be made much more flexible using the arguments
array:
function f1() {
var i;
// ...
for (i = 0; i < arguments.length; ++i)
{
if (typeof arguments[i] === 'function')
{
arguments[i]();
}
}
}
Like this, you can pass in as many callback functions as you want. They will be executed in the order passed in. No harm is done if the argument is not a callback.
Finally, you can preserve the context and arguments of f1
using apply()
.
Preserving the context could definitely be useful in many situations. I'm not sure about the arguments, but it is an option.
function f1() {
var i;
// ...
for (i = 0; i < arguments.length; ++i)
{
if (typeof arguments[i] === 'function')
{
// You can leave off 'arguments', but preserving 'this' will
// often be useful.
arguments[i].apply(this, arguments);
}
}
}
the above allows you to do things like:
f1("alert me",function() {alert(arguments[0]);});
// Output when the call back is called:
// "alert me"
jsFiddle example
本文标签:
版权声明:本文标题:javascript - I have function, f1, and I want to fire a call back when f1 is complete, how to do this? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322008a2452977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论