javascript – 没有UI块的处理函数链
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 没有UI块的处理函数链,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2898字,纯文字阅读大概需要5分钟。
内容图文
我需要在我的JavaScript / jQuery中执行几个函数,但我想避免阻止UI.
AJAX不是一个可行的解决方案,因为应用程序的性质,这些功能将很容易达到成千上万.异步执行此操作会终止浏览器.
因此,我需要某种方式来链接浏览器需要处理的函数,并且只在第一个函数完成后发送下一个函数.
算法是这样的
For steps from 2 to 15
HTTP:GET amount of items for current step (ranges somewhere from a couple of hundred to multiple thousands)
For every item, HTTP:GET the results
如你所见,我有两个GET请求 – “链”我不知何故需要管理…尤其是最里面的循环立即崩溃浏览器,如果它是异步完成的 – 但我仍然希望用户能够操作页面,因此纯(阻塞)同步方式不起作用.
解决方法:
您可以轻松地异步执行此操作,而无需立即触发所有请求.您需要做的就是管理队列.为清楚起见,以下是伪代码.它很容易转换为真正的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"
}
}
}
内容总结
以上是互联网集市为您收集整理的javascript – 没有UI块的处理函数链全部内容,希望文章能够帮你解决javascript – 没有UI块的处理函数链所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。