排队 AJAX 调用
[英] Queue AJAX calls
本文介绍了排队 AJAX 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在做一个水平滚动网站,例如:http://vanityclaire.com/
但是,在加载主页之后,我没有使用一个大的 HTML 文件,而是使用 jQuery .load() 在 home 的孩子中进行 ajaxing.
目前我在标题中的 url 中为每个 div 和 ajax.但是 AJAX 返回乱序,当我添加更多页面时,我不希望跨越服务器有 30 多个 http://请求.
我如何同步执行 AJAX 调用,即在请求另一个之前等待第一个返回,甚至一次发送两个.
我一直在搜索,无法弄清楚我需要什么.
这是我的 HTML:
<div class="scrollItems"><div id="page-1" class="scrollItem" title="/"><div>主页内容</div>
<div id="page-2" class="scrollItem" title="/Page2.html"><div class="加载">
<div id="page-3" class="scrollItem" title="/Page3.html"><div class="加载">
<div id="page-4" class="scrollItem" title="/Page4.html"><div class="加载">
<div id="page-5" class="scrollItem" title="/Page5.html"><div class="加载">
还有我的 JS:
function s_loadingInitialPages() {var loadingItems = new Array();$(".scrollArea .scrollItem").each(function () {if ($(this).attr('title') != '/') {var oDelem = $(this);loadingItems.push(oDelem);//警报('测试');}});for (i = 0; i
有没有插件可以让我继续向队列添加函数,然后让它处理它?</p>
解决方案
诀窍是使用回调.您进行一次 ajax 调用,并在其成功回调时进行下一次调用.
要做到这一点,只需将它们全部添加到一个队列中,并在其周围使用一个包装器来一个一个地发送它们.
我几天前写了一篇.稍后我将向您展示一个实现.
//缓冲区类.有一个公共 append 方法,它需要某种任务.//构造函数需要一个处理程序,它是一个接受 ajax 任务的方法//和一个回调.Buffer 期望处理程序处理 ajax 并运行//完成时的回调函数缓冲区(处理程序){var 队列 = [];函数运行(){var 回调 = 函数 () {//当处理程序说它完成时(即运行回调)//我们检查队列中是否有更多任务,如果有我们再次运行如果(队列长度> 0){跑();}}//给出队列中的第一项 &对处理程序的回调处理程序(queue.shift(),回调);}//将任务推入队列.如果在推送任务之前队列为空//我们运行任务.this.append = 函数(任务){队列推送(任务);if (queue.length === 1) {跑();}}}//包含 item & 的小任务网址 &可选回调功能任务(项目,网址,回调){this.item = 项目;this.url = url;this.callback = 回调}//将 task.url 加载到 task.item 并调用回调的小处理程序//当它完成时函数 taskHandler(任务,回调){$(task.item).load(task.url, function() {//从任务中调用一个选项回调if (task.callback) task.callback();//调用缓冲区回调.打回来();});}//创建一个带有任务处理程序的缓冲区对象var buffer = new Buffer(taskHandler);for (i = 0; i
为代码墙道歉.只需实现您自己的任务和处理程序.只要处理程序在完成处理任务后调用第二个参数(回调),缓冲区就会工作.
然后只需向它传递一个任务和一个处理程序.处理程序执行 ajax 并在 ajax 返回时从缓冲区调用回调.
对于您的具体示例,如果您的加载需要很长时间,那么加载所有 30 个需要很长时间.ajax 的重点是让服务器并行执行操作.
在您的情况下,更好的解决方案是发出 30 个请求,然后捕获结果并确保 ajax 调用的结果仅按顺序附加到 dom.这涉及使用 $.ajax 并以某种方式添加跟踪订单.
这样服务器就会尽可能快地完成它,一旦你得到它,你就可以按顺序提供它.或者,如果你做的事情很快,那么在缓冲区中排队也没有损失.
Hello I am doing a Horizontal scrolling website like: http://vanityclaire.com/
However, rather than having one large HTML file, after the load of the homepage, I am ajaxing in the children of home, using jQuery .load().
At present I for-each div and ajax in ithe url that sits in the title. But the AJAX returns out of order, and as I add more pages don't fancy spanging the server with 30+ http:// requests.
How do I synchronously do the AJAX calls, i.e. wait for the first to comeback before a request another, or even send two at a time.
I have been scouring, and cannot figure out what I need.
This is my HTML:
<div id="mainLayout" class="fullwidth scrollArea">
<div class="scrollItems">
<div id="page-1" class="scrollItem" title="/">
<div>HOME PAGE CONTENT</div>
</div>
<div id="page-2" class="scrollItem" title="/Page2.html">
<div class="loading"> </div>
</div>
<div id="page-3" class="scrollItem" title="/Page3.html">
<div class="loading"> </div>
</div>
<div id="page-4" class="scrollItem" title="/Page4.html">
<div class="loading"> </div>
</div>
<div id="page-5" class="scrollItem" title="/Page5.html">
<div class="loading"> </div>
</div>
</div>
</div>
And my JS:
function s_loadingInitialPages() {
var loadingItems = new Array();
$(".scrollArea .scrollItem").each(function () {
if ($(this).attr('title') != '/') {
var oDelem = $(this);
loadingItems.push(oDelem);
//alert('test');
}
});
for (i = 0; i < loadingItems.length; i++) {
// title attribute is the URL to get
var ajaxURL = loadingItems[i].attr("title") + '?ajaxPageContent=';
$(loadingItems[i]).load(ajaxURL);
}
}
Is there a plugin I can just keep adding functions to a queue, and let that handle it?
解决方案
The trick is to use the callbacks. You make one ajax call and on its success callback you make the next one.
To do this just add them all to a queue and have a wrapper around it that sends them one by one.
I wrote one a few days ago. I'll show you an implementation in a second.
// Buffer class. Has a public append method that expects some kind of Task.
// Constructor expects a handler which is a method that takes a ajax task
// and a callback. Buffer expects the handler to deal with the ajax and run
// the callback when it's finished
function Buffer(handler) {
var queue = [];
function run() {
var callback = function () {
// when the handler says it's finished (i.e. runs the callback)
// We check for more tasks in the queue and if there are any we run again
if (queue.length > 0) {
run();
}
}
// give the first item in the queue & the callback to the handler
handler(queue.shift(), callback);
}
// push the task to the queue. If the queue was empty before the task was pushed
// we run the task.
this.append = function(task) {
queue.push(task);
if (queue.length === 1) {
run();
}
}
}
// small Task containing item & url & optional callback
function Task(item, url, callback) {
this.item = item;
this.url = url;
this.callback = callback
}
// small handler that loads the task.url into the task.item and calls the callback
// when its finished
function taskHandler(task, callback) {
$(task.item).load(task.url, function() {
// call an option callback from the task
if (task.callback) task.callback();
// call the buffer callback.
callback();
});
}
// create a buffer object with a taskhandler
var buffer = new Buffer(taskHandler);
for (i = 0; i < loadingItems.length; i++) {
// title attribute is the URL to get
var ajaxURL = loadingItems[i].attr("title") + '?ajaxPageContent=';
buffer.append(new Task(loadingItems[i], ajaxURL));
}
Apologies for the wall of code. Just implement your own Task and Handler. The Buffer will work as long as the handler calls the second argument (the callback) when it's finished handling the task.
Then just pass it a task and a handler. The handler does the ajax and calls the callback from the buffer when the ajax returns.
For your specific example if what your loading takes a long time then this will take a long time to load all 30. The point of ajax is to have the server do stuff in parallel.
A far better solution in your case is to make 30 requests and then catch the results and make sure the results from your ajax calls are only appended to the dom in order. This involves using $.ajax and adding keeping track of order somehow.
That way the server will do it as fast as it can and you can server it in order once you get it. Alternatively if the things your doing are fast then queuing them in a buffer has no penalty.
这篇关于排队 AJAX 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文