jQuery Async Call only"parallel"(并行);网址不同时 [英] jQuery Async Call only "parallel" when URL's different

查看:75
本文介绍了jQuery Async Call only"parallel"(并行);网址不同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我在玩jQuery和异步调用,发现了一个不寻常的问题.我想在一个循环中运行多个Ajax调用.我写了下面的内容(其中rand.php只睡了一秒钟,然后返回一个随机数).令人惊讶的是,它可以同步执行,大约需要20秒才能完成.

I was playing with jQuery and async calls last night and found an unusual issue. I wanted to run multiple Ajax calls inside a loop. I wrote the below (where rand.php just sleeps for a second and returns a random number). Somewhat surprisingly it executes synchronously and takes 20 seconds or so to finish.

$(document).ready(function () {
    $([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]).each(function() {
        var number = this;

        $.get("rand.php", function(data) {
            $('#'+number).html(data);
        });
    });
});

PHP代码如下,

<?php
sleep(1);
echo rand(); 
?>

我当时想这显然是错误的,因为异步调用应该没有阻塞,并且几乎是并行返回的.经过大量的尝试(假设这是服务器问题),我发现将任何内容附加到URL上看起来像是与众不同的都是按预期的方式工作的.也就是说,它会在3秒左右的时间内返回(一次调用约6次).

I was thinking this is clearly wrong as the async calls should be no blocking and return almost in parallel. After much playing around (assuming it was a server issue) I discovered that appending anything to the URL to make it look like it was different worked as expected. That is it returned in 3 seconds or so (6 or so calls at a time).

$(document).ready(function () {
    $([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]).each(function() {
        var number = this;

        $.get("rand.php?"+number, function(data) {
            $('#'+number).html(data);
        });
    });
});

我不认为jQuery/Javascript大师可以解释这种行为吗?这是浏览器的一些限制吗?为什么只有在URL不同的情况下才能按预期运行?

I don't suppose a jQuery/Javascript Guru can explain this behavior? Is it some browser limitation? Why is it that only when the URL's are different that it runs as I would expect?

编辑-而不是回复,它使用的是Chrome(无论最新版本)和Firefox 5/6.我确实在可缓存它的IE中进行了尝试,因此忽略了它,而将精力集中在Chrome上.有趣的是,第一个页面在IE9中按预期的方式在第一次加载时就可以正常工作,但是在重新加载时仅显示缓存的结果.

EDIT - Rather then reply, this was using Chrome (whatever the latest is) and Firefox 5/6. I did try it in IE which did cache it, so ignored that and focused on Chrome. Interesting the first one works as expected in IE9 on the first page load, but then just displays cached results when reloaded.

推荐答案

您在,即不是.坏的!坏的IE用户! IE缓存将获取请求,就好像它是其他任何内容一样. jQuery具有一个内置函数来解决此问题:

You are in ie aren't you. Bad! Bad IE user! IE caches get requests as if it was any other content. Jquery has a built in function to address this:

$.ajaxSetup({ cache : false });

这将增加一个漂亮的扰流器来解决这个问题.但是,为什么要在其他浏览器中添加扰流器呢?所以通常我这样做:

This will add a nifty spoiler to take care of this. But why add the spoiler in other browsers? So usually I do this:

if(!+"\v1"){
    $.ajaxSetup({ cache : false });
}

这是IE的测试,仅在该浏览器中进行设置.

Which is the tests for IE and set it only in that browser.

这篇关于jQuery Async Call only"parallel"(并行);网址不同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆