2016-06-29 53 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;同樣的結果 –