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?
In direct answer to your question, this does not work:
That will execute
callbackfunctionimmediately and pass the return value from executing it as the argument tofirstfunctionwhich is unlikely what you want.It is unclear from your question whether you should just change
firstfunction()to pass two parameters tocallbackfunction()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:
or