2016-06-29 50 views
0

下面的JavaScript函数每15秒运行一次并调用ASP.Net Page方法。大约需要4到8秒才能完成。如何防止ASP.net PageMethod调用阻止其他JavaScript函数调用

我还在同一页面上运行另一个JavaScript函数,每2秒运行一次,但会被以前需要更长时间才能完成的方法定期阻止。

function get_case_list_data(count, max_id) { 
    PageMethods.GetCaseList(count, max_id, uid, is_agent, got_case_list_data, OnFailure); 
} 

请如何防止ASP.Net页面方法调用阻止其他JavaScript函数在同一页上执行?

回答

1

使用浏览器调试工具并检查PageMethods.GetCaseList中使用的自动生成的代码,然后仅使用异步ajax调用而不是阻塞调用来模仿调用。

PageMethods包装只是为了方便,但代码通常很丑。你可以随时用$ .ajax或本机XmlHttpRequest手动调用它。

async = true;

如果您进行多次调用,ASP.NET会话可能会进行阻止。使用警报或CONSOLE.LOG的JavaScript方法中,以确定是什么原因阻止

function get_case_list_data(count, max_id) { 
    console.log("before call"); 
    PageMethods.GetCaseList(count, max_id, uid, is_agent, got_case_list_data, OnFailure); 
    console.log("after call"); 
} 

function got_case_list_data(){ 
    console.log("in PageMethod success"); 
    // -- updated -- 
    // this could be blocking the call to/from other 2 second timer 
    // JS is single thread, so window.timeout and ajax callbacks will 
    // wait until the function is exited 
    // -- end update-- 
    console.log("end of PageMethod success"); 
} 

- updated--

设置asp.net会话为只读去除独占会话锁,将同步线程

+0

所以基本上这些调用不是异步的? –

+0

您可以在PageMethod调用之前和之后以及成功回调中使用警报来测试。谷歌“aysnc PageMethods”看起来可能是aysnc默认情况下,你需要进行更新,使其syncronouse。无论哪种方式,这个博客有一些很好的内容让你开始。 http://abhijit-j-shetty.blogspot.com/2011/04/aspnet-ajax-calling-pagemethods.html – Steve

+0

尝试使用async = true的$ ajax;同样的结果 –