Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  34] [ 5]  / answers: 1 / hits: 57850  / 12 Years ago, mon, october 22, 2012, 12:00:00

This question looks like a duplicate, as the title is nearly replicated. But, my issue seems simpler and I can't find the answer to it.



I have a Javascript function that executes another callback function, it works like this:



<script type='text/javascript'>
firstfunction(callbackfunction);
</script>


where callback function is defined as:



callbackfunction(response) {
if (response=='loggedin'){
// ... do stuff
}}


but I want it to be something like this:



callbackfunction(response, param) {
if (response=='loggedin'){
// ... do stuff with param
}}


My question is, does it work to pass the parameter like this:



<script type='text/javascript'>
firstfunction(callbackfunction(param));
</script>


or am I doing it wrong?


More From » callback

 Answers
3

In direct answer to your question, this does not work:



 firstfunction(callbackfunction(param));


That will execute callbackfunction immediately and pass the return value from executing it as the argument to firstfunction which is unlikely what you want.






It is unclear from your question whether you should just change firstfunction() to pass two parameters to callbackfunction() when it calls the callback or whether you should make an anonymous function that calls the callback function with arguments.



These two options would look like this:



function firstfunction(callback) {
// code here
callback(arg1, arg2);
}

firstfunction(callbackfunction);


or



function firstfunction(callback) {
// code here
callback();
}

firstfunction(function() {
callbackfunction(xxx, yyy);
});

[#82440] Friday, October 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;