Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PageMethod的OnSuccess函数中未定义Javascript全局数组?_Javascript_Asynchronous_Callback_Closures_Pagemethods - Fatal编程技术网

在PageMethod的OnSuccess函数中未定义Javascript全局数组?

在PageMethod的OnSuccess函数中未定义Javascript全局数组?,javascript,asynchronous,callback,closures,pagemethods,Javascript,Asynchronous,Callback,Closures,Pagemethods,我使用pagemethods在页面上异步加载多个用户控件。一些dropdownlists会导致这些usercontrols的异步回调,用户控件会重新加载修改过的内容。我们有这样做的理由 但是,目前我们的用户必须等待这些用户控件加载,然后才能更改DropDownList中的选择。为了提供更好的用户体验,我尝试中止以前的请求,但尚未加载 我试图维护一个由杰出的异步请求执行器组成的全局js数组,以确保只加载每个用户控件的最新请求。换句话说,我想取消以前对特定usercontrol的请求,这些请求尚未加

我使用pagemethods在页面上异步加载多个用户控件。一些dropdownlists会导致这些usercontrols的异步回调,用户控件会重新加载修改过的内容。我们有这样做的理由

但是,目前我们的用户必须等待这些用户控件加载,然后才能更改DropDownList中的选择。为了提供更好的用户体验,我尝试中止以前的请求,但尚未加载

我试图维护一个由杰出的异步请求执行器组成的全局js数组,以确保只加载每个用户控件的最新请求。换句话说,我想取消以前对特定usercontrol的请求,这些请求尚未加载,并为该usercontrol的最新请求赋予优先级

我的问题是,在执行OnSuccess函数时,全局数组尚未定义

我是不是走错了路?我不知道的是什么

任何帮助都将不胜感激

这是我的代码的简化示例:

var outstanding_requests; 

$.fn.remove_or_item = function (val, col) { 
    var arr1 = $(this); 
    var arr2 = new Array(); 
    $.each(arr1, function (k, v) { 
        if (v[col] != val) { arr2.push(v); } 
        else if (v[1].get_started()) { v[1].abort(); } 
    }); 
    return arr2; 
}

$.fn.find_or_item = function (val, col) { 
    var item; 
    var arr1 = $(this); 
    $.each(arr1, function (k, v) { 
        if (v[col] == val) { item = v; return false; } return true; 
    }); 
    return item; 
}

function RunMyPageMethod(panelname) {
    var request; //current request object
    if (outstanding_requests == undefined) { outstanding_requests = new Array(); }
    outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
    request = PageMethods._staticInstance.LoadUserControl(panelname, PageMethodSuccess, PageMethodFailure);
    outstanding_requests.push([panelname, request.get_executor()]);
}

function PageMethodSuccess(result, userContext, methodName) {
    var panelname = result.split("|")[0];
    //here outstanding_requests is undefined
    if($(outstanding_requests).find_or_item(panelname,0))
    {
        outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
        //load usercontrol
    }
}

数组在js中是按引用传递的。这样做的好处是允许我查看数组在我点击OnSuccess函数时的状态,而不是调用pagemethod时的状态。至少我认为这就是它起作用的原因。我需要将对数组的引用传递到OnSuccess函数中。最后我用上面显示的最后两个函数完成了这项工作,这两个函数工作得很好

function RunMyPageMethod(panelname) {
    var request; //current request object
    if (outstanding_requests == undefined) { outstanding_requests = new Array(); }
    outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
    request = PageMethods._staticInstance.LoadUserControl(panelname,function(result,userContext,methodName){ PageMethodSuccess(result,userContext,methodName,outstanding_requests); }, PageMethodFailure);
    outstanding_requests.push([panelname, request.get_executor()]);
}

function PageMethodSuccess(result, userContext, methodName, outstanding_requests) {
    var panelname = result.split("|")[0];
    if($(outstanding_requests).find_or_item(panelname,0))
    {
        outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
        //load usercontrol
    }
}
我发现了,但我不确定当OnSuccess函数名作为pagemethod调用的参数(与函数本身类似)提供时,为什么闭包不起作用?