I need to write a function:
function doTestConnCall(param1, param2, callbackfun)
param1 & param2 are parameters which I have used inside the function. The 3rd parameter - callbackfun
is a function which to be called after finishing doTestConnCall
doTestConnCall(param1,callback1,callback2)
Think I am missing some basics. Could any one lead me
You can do something like this:
callbackfun(argument1, argument2);
or:
callbackfun.apply(this, [ argument1, argument2 ]);
or:
callbackfun.call(this, argument1, argument2);
The same can be done with multiple callbacks. For example:
callback1.call(this);
callback2.call(this);
See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply
And: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/call
Call any callback whenever appropriate in the function you are writing--in the case you described, after it completes its core work.
Yes, of course. Call them one after another.
function multipleCallbacks(arg, arg, callback1, callback2) { // Do some stuff // Do error checking in the real world if you need to be tolerant callback1(); callback2(); }
Functions in JS are top level constructs. That means function hello() {}
is the same as var hello = function() {};
. So if you want to pass hello
to the function above, you can just call doTestConnCall(param1, param2, hello)
after you have defined hello()
using either method above.
This is how you achieve it.
It is possible to pass what ever you want to as a method parameter.
function doTestConnCall(param1, param2, callbackfun){
DO YOUR LOGIC
callbackfun() // CALL YOUR CALLBACK
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.