栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

没有UI块的功能处理链

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

没有UI块的功能处理链

您可以轻松地异步执行此操作,而无需一次触发所有请求。您需要做的就是管理队列。为了清楚起见,以下是伪代码。它很容易转换为真实的AJAX请求:

// Basic structure of the request queue. It's a list of objects// that defines ajax requests:var request_queue = [{    url : "some/path",    callback : function_to_process_the_data}];// This function implements the main loop.// It looks recursive but is not because each function// call happens in an event handler:function process_request_queue () {    // If we have anything in the queue, do an ajax call.    // Otherwise do nothing and let the loop end.    if (request_queue.length) {        // Get one request from the queue. We can either        // shift or pop depending on weather you prefer        // depth first or breadth first processing:        var req = request_queue.pop();        ajax(req.url,function(result){ req.callback(result); // At the end of the ajax request process // the queue again: process_request_queue();        }    }}// Now get the ball rolling:process_request_queue();

因此,基本上,我们将ajax调用本身转换为伪循环。基本上,这是递归完成的经典延续编程风格。

在您的情况下,请求的示例为:

request_queue.push({    url : "path/to/OUTER/request",    callback : function (result) {        // You mentioned that the result of the OUTER request        // should trigger another round of INNER requests.        // To do this simply add the INNER requests to the queue:        request_queue.push({ url : result.inner_url, callback : function_to_handle_inner_request        });    }});

这非常灵活,因为您不仅可以选择以广度优先还是深度优先(shift与pop)处理请求。但是,您也可以使用splice将内容添加到队列的中间,或者使用unshift
vs push将请求放置在队列的开头以处理高优先级的请求。

您还可以通过在每个循环中弹出多个请求来增加同时请求的数量。只需确保

process_request_queue
每个循环仅调用一次即可避免同时请求呈指数增长:

// Handling two simultaneous request channels:function process_request_queue () {    if (request_queue.length) {        var req = request_queue.pop();        ajax(req.url,function(result){ req.callback(result); // Best to loop on the first request. // The second request below may never fire // if the queue runs out of requests. process_request_queue();        }    }    if (request_queue.length) {        var req = request_queue.pop();        ajax(req.url,function(result){ req.callback(result); // DON'T CALL process_request_queue here // We only want to call it once per "loop" // otherwise the "loop" would grow into a "tree"        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616667.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号