jQuery捕获img

jQuery捕获img,jquery,Jquery,我们为每个项目使用了一个脚本 $(".item").each(function(){ item_link = "http://..."; block = $('.block', this); $.get(item_link, function(data) { var src = $('img.slide', data).attr('src'); block.html(src); }); }); item\u link变量对于每个查

我们为每个
项目使用了一个脚本

$(".item").each(function(){
    item_link = "http://...";
    block = $('.block', this);
    $.get(item_link, function(data) {
        var src = $('img.slide', data).attr('src');
        block.html(src);
    });
});
item\u link
变量对于每个查询都是唯一的

可以有100个
.item
或更多

问题是-服务器同时对连接有限制,这就是为什么有些
.item
get
var src
,有些则没有

最好的解决方案是只使用一个
。同时获取
。我认为应该有一些计数器,如果
.get
完成-它会向下一个
.get
发送消息“我完成了,您可以开始执行”,以此类推

怎么做?

谢谢。

浏览器应自动将请求排队,并在前两个请求之一完成后发送每个请求

因此,您当前的代码应该可以正常工作。
检查Fiddler或Firebug,查看请求是否失败

编辑 您正在与服务器节流而不是浏览器的连接限制作斗争

您需要维护一个发送请求的函数数组,然后调用AJAX回调中的下一个函数

例如:

var requests = [];
function runFirstRequest() {
    if (requests.length > 0)
        requests.splice(0, 1)();  //Pop the first function from the queue and execute it
}


$(".item").each(function(){
    item_link = "http://...";
    block = $('.block', this);

    requests.push(function() {
        $.get(item_link, function(data) {
            var src = $('img.slide', data).attr('src');
            block.html(src);

            runFirstRequest();    //Send the next request
        });
    });
});

runFirstRequest();

但是,发送数百个AJAX请求是个坏主意。
您应该修改代码以发送一个AJAX请求,该请求返回您需要的所有数据。
这将解决您的问题,并使页面更快。

浏览器应自动将请求排队,并在前两个请求之一完成后发送每个请求

因此,您当前的代码应该可以正常工作。
检查Fiddler或Firebug,查看请求是否失败

编辑 您正在与服务器节流而不是浏览器的连接限制作斗争

您需要维护一个发送请求的函数数组,然后调用AJAX回调中的下一个函数

例如:

var requests = [];
function runFirstRequest() {
    if (requests.length > 0)
        requests.splice(0, 1)();  //Pop the first function from the queue and execute it
}


$(".item").each(function(){
    item_link = "http://...";
    block = $('.block', this);

    requests.push(function() {
        $.get(item_link, function(data) {
            var src = $('img.slide', data).attr('src');
            block.html(src);

            runFirstRequest();    //Send the next request
        });
    });
});

runFirstRequest();

但是,发送数百个AJAX请求是个坏主意。
您应该修改代码以发送一个AJAX请求,该请求返回您需要的所有数据。

这将解决您的问题,并使页面更快。

您应该将它们全部放在一个大请求中,让服务器循环通过它们:

items_ids = [];
$(".item").each(function(){
    item_ids.push( some_identifier_for_the_item );
});

get_item_data_link = "http://...";
$.get(item_link, {"items": item_ids}, function(data) {
    ... loop through the results and stick them in the page
});
PS-如果您仍然设置请求队列,请使用push、pop、shift和unshift

先进先出队列:

queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the oldest item off the queue (ie. the first item
// added is the first item handled)

queue.shift(); // "item1"
queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the newest item off the queue (ie. the last item
// added is the first item handled)

queue.pop(); // "item3"
后进先出队列:

queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the oldest item off the queue (ie. the first item
// added is the first item handled)

queue.shift(); // "item1"
queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the newest item off the queue (ie. the last item
// added is the first item handled)

queue.pop(); // "item3"

您应该将它们放在一个大请求中,让服务器循环通过它们:

items_ids = [];
$(".item").each(function(){
    item_ids.push( some_identifier_for_the_item );
});

get_item_data_link = "http://...";
$.get(item_link, {"items": item_ids}, function(data) {
    ... loop through the results and stick them in the page
});
PS-如果您仍然设置请求队列,请使用push、pop、shift和unshift

先进先出队列:

queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the oldest item off the queue (ie. the first item
// added is the first item handled)

queue.shift(); // "item1"
queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the newest item off the queue (ie. the last item
// added is the first item handled)

queue.pop(); // "item3"
后进先出队列:

queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the oldest item off the queue (ie. the first item
// added is the first item handled)

queue.shift(); // "item1"
queue = [];

// add 3 items to the queue
queue.push('item1');
queue.push('item2');
queue.push('item3');

// get the newest item off the queue (ie. the last item
// added is the first item handled)

queue.pop(); // "item3"

这不应该发生。你有任何错误吗?我得到“503服务暂时不可用”这是不应该发生的。您是否收到任何错误?我收到“503服务暂时不可用”有许多get请求,其中许多请求都给出了“503服务暂时不可用”我强烈建议您尝试更改系统,以允许您合并请求。(使用服务器端代码)您应该调试代码并找出错误所在。如果您需要帮助,请向我们显示您的确切代码。您忘记调用
runFirstRequest()填充队列后。有很多GET请求,其中很多都会给出“503服务暂时不可用”我强烈建议您尝试更改系统以允许合并请求。(使用服务器端代码)您应该调试代码并找出错误所在。如果您需要帮助,请向我们显示您的确切代码。您忘记调用
runFirstRequest()填充队列后。