异步AJAX按顺序调用

Asynchronous AJAX Calls in orderd manner

本文关键字:顺序调用 AJAX 异步      更新时间:2023-09-26

你好,首先为我糟糕的英语道歉。我已经在SO中搜索了。但我没有得到我想要的确切答案。我的问题是我需要同步的Ajax请求。我知道我们可以使用"asynch: false "。但这将使浏览器锁定。我有一个文件夹树(我使用"tafel树"js)在我的web。树节点是在运行时生成的。每一次用户单击一个节点,它将向服务器发送请求并将该节点添加到树中。但问题是,如果页面刷新点击f5然后我需要加载我之前已经选择的树结构。我使用"asynch: false "实现了它。但是这会使浏览器太慢。

这里是

function loadPage() { 
/* some other codes are here*/
/* here i call an ajax for get the inside folder list in correct order.(i am usig     protoype)*/
    new Ajax.Request(ajaxGetInsideFolderIdsUrl,
    {
     parameters: {branchId: CurrentSelectfolderId},
     onSuccess:  function(res) {
            /* onSuccess i call another function*/
            var brs = res.responseText.split(","); // branch ids in correct order.
            syncFolder(brs)
    }
}
function syncFolder(brs){
for(var i= 0 ; i < brs.length; i ++){  
    var tempbranch = tree.getBranchById(brs[i].getId());              
    selectAfterChange( tempbranch) 
    /* 
     selectAfterChange function is used for selecting current branch. calling "tafle  tree" select() function in side it.
     i just created an copy of "select()","_openPopulate()" functions used in "tafle  tree" and modified it with "asynch : false ".and now its working fine.
*/  
}
}
function selectAfterChange(brs){
    brs.chk_select(); 
    /* for selecting the branch (created a copy of current "select()" function used in "tafle tree" js  
    and modified it with "asynch : false "and now its working fine.) */
    showList();// for listing items in side that folder (in another Ajax page). 
}

我的问题是如果用户打开了一个长分支。然后刷新页面,这将花费太多时间来加载,因为同步Ajax调用。花太多时间对我来说不是什么大问题。但是浏览器会被锁定,直到所有的请求都被执行。还有其他方法吗?

我不熟悉您正在使用的树库,但一般来说,您解决这个问题的方法是将当前扩展的路径存储在浏览器中(本地存储,或cookie,或其他地方),当您刷新页面时,只加载可见的节点。

假设用户当前正在查看路径1/2/3/4。您将该路径保存在某个地方,然后当页面再次加载时,通过拆分路径并逐个附加路径的组件,创建要从后端请求的路径队列:

["one", "one/two", "one/two/three"]

然后发送"one"的AJAX请求,当您获得结果返回时,更新树中的节点,并发送"one/two"的请求。当该请求返回时,更新树,并发送"one/two/three"的请求。

根据你的设计,你可以开始用更多的异步请求填充树的其余部分…