Javascript 具有函数返回请求数据+;要执行的函数

Javascript 具有函数返回请求数据+;要执行的函数,javascript,Javascript,我有一个函数,它获取一个html文件并将其显示在页面上,但我想告诉它在处理完页面后也执行一个函数。除了我不知道如何传递数据 这就是我目前拥有的: call_file('index.html', function(){ template_handler( function(){ init_menu(); } )} ); } 这是这样处理

我有一个函数,它获取一个html文件并将其显示在页面上,但我想告诉它在处理完页面后也执行一个函数。除了我不知道如何传递数据

这就是我目前拥有的:

call_file('index.html', function(){ 
                        template_handler(
                            function(){ init_menu(); }
                        )}
    );
}
这是这样处理的:

function call_file(url,func){
    caller(url,function(){
        if ( xmlhttp.readyState== 4 && xmlhttp.status== 200 ){
            call_back_fileLoad(xmlhttp.responseText,func);
        }
    });
}
function call_back_fileLoad(result,func){

        if(typeof(func) !== 'undefined'){       
            func(result);
        } else {
            return false;
        }
}

function caller(url,cfunc){
        if (window.XMLHttpRequest)
          {
            xmlhttp=new XMLHttpRequest();
          }
        else
          {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
            xmlhttp.onreadystatechange=cfunc;
            xmlhttp.open("GET",url,true);
            xmlhttp.send();
}
问题是我不知道如何接收html&我想同时执行的函数,因为它只向函数返回一段数据

这就是我希望最终职能部门采取的行动:

function template_handler(data, func){
    var d = document.getElementById('content');
        d.innerHTML = '';
    var div = document.createElement(div);
        div.innerHTML = data;

    d.appendChild(div);

    func(); 
}
目前我不知道如何正确传递
call_file
的数据,请帮助^ ^ ^

如何

编辑
正如我在上述评论中指出的:


只需为要传递给
call\u file()
的回调函数定义一个参数,然后将该参数作为第一个参数传递给
template\u handler()
。如下所示:


顺便说一句,您可以去掉最里面的匿名函数,只需传递
init_menu
,因为它看起来好像不需要参数

call_file('index.html', function(data){ 
    template_handler(data, init_menu);
});

不要给函数命名
调用方
。无法在全局上下文之外调用。只需为要传递给
call\u file()
的回调函数定义一个参数,然后将该参数作为第一个参数传递给
template\u handler()
。类似这样:
call_file('index.html',function(data){template_handler(data,function(){init_menu();}})}@CrazyTrain我不太明白?我是否必须更改请求函数中的任何内容?@sabof it使用该名称=/@Dave:我在我的评论中添加了一个示例。传递到
call\u file
的回调变为
function(data){…
,而
template\u处理程序的调用变为
template\u处理程序(data,function(){init…
我不需要为thatnope.更改我的
call_file
函数吗?因为这个实现是在以后调用的后续函数中。我是否缺少任何东西..@CrazyTrain
xmlhttp.onreadystatechange=cfunc;
在一个成功的请求中将调用5次,我的下一个函数正在处理后一部分
如果(xmlhttp.readyState==4&&xmlhttp.status==200){
确定如果您正在执行
xmlhttp.onreadystatechange=cfunc;
,它将被多次调用,并将根据
.readyState
进行过滤,但这不是您正在执行的操作。您将函数传递给
调用方()
.Edited,这有意义吗?甚至不确定它是否有效。只是一个疯狂的尝试确定后者对0:)这使它变得很好和简单!谢谢!@Dave:让我们把讨论移到这里。你是提出了几个请求,还是仅仅提出了一个请求?调用
call_file
是否被多次调用?它调用了一次,但有时当我刷新它时,它只执行一次,另一次5=/看起来你好像在使用
xmlhttp
的共享变量。我想知道是否没有his起到了一定的作用。转到传递给调用方的回调函数,将3个
xmlhttp
变量改为
this
。因此
if(this.readyState==4&&…
等等。我解决了它!如何设置初始加载页是一个复杂的问题。我的错!:P
// `call_back_fileLoad()` passes the response, so receive it here
// --------------------------------v
call_file('index.html', function(data) {
    // Then pass it on to the `template_handler()` invocation
    // ----------------v
    template_handler(data, function() { 
        init_menu(); 
    });
});
call_file('index.html', function(data){ 
    template_handler(data, init_menu);
});