0
<script type="text/javascript">
    var sourceData = {sites:[{ name: "London" , Id: "0"}, { name: "Bath", Id: "1"}, { name: "Cambridge", Id: "2"}]};
    $(document).ready(function () {
    $(sourceData.sites).each(function (index, data) { var objName = "<div class='bold' id = place" + data.Id + ">" + data.name + "</div>"; $('#results').append(objName); });

    $(sourceData.sites).each(function (index, data) {
    $.getJSON("GenerateData.aspx?place=" + data.name, function (retData) {
            $('#place' + data.Id)
                .wrap("<a href=GenerateData.aspx?place=" + data.name + "></a>")
                .append(retData.Wait); });
        });
    });
</script>

So basically generateData just returns a json which looks like:

{"name": "(place parameter passed back)", "Wait": 5000}

Except 5000 is actually Rand(0,5000) and also corresponds to a thread.sleep that executes before the request is returned.

What I can't understand is that while I'm getting some parallelisation (the order in which results are returned is random), the order in which they return is not from lowest to highest, and also the total return time is equal to the total wait times.

So there's no actual parallelisation of the requests against GenerateData, even though the requests seem to be sent out in parallel.

I've seen Asynchronous Controller is blocking requests in ASP.NET MVC through jQuery but that seems specific to MVC which I'm not using (yet).

Am I doing anything wrong from the jquery side, or is this another case of ASP.net sessions messing parallelisation up?

look at the latencies in the javascript console

4

1 回答 1

1

$.getJSON()异步操作并非巧合。请求响应不会以与请求相同的方式排序,或者至少很少会这样排序。如果您希望您的请求按照您发送它们的顺序返回,那么您需要创建一个 Ajax 队列,然后请求将在之前的请求完成后发送。您可以使用jQuery.queue().

于 2013-02-12T13:07:01.080 回答