简体   繁体   中英

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 ?

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM