TeAmEr
TeAmEr

Reputation: 4773

how to send js function as parameter to another function?

I have the following code

function exec(param1,p2,fun){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(fun){
        data=xmlhttp.responseText;
        fun(data);
    }
    // rest ajax connection code
}

when i call

 exec(param1,param2,
    function(data){
      alert (data);
    });

it says

Object not a function

in the definition at line fun('test');

any ideas ?

Upvotes: 0

Views: 115

Answers (1)

Quentin
Quentin

Reputation: 943979

You are overwriting fun with a new one in a narrower scope:

function exec(param1,p2,fun){
                        ^^^  - The function you pass

xmlhttp.onreadystatechange=function(fun){
                                    ^^^ - new variable (possibly an event object)

Change one of the variable names so you aren't masking it inside your callback function.

Upvotes: 1

Related Questions