3

我注意到请求文件的大小会影响 ajax 调用的响应时间。因此,如果我对不同大小的文件发出 3 个 ajax GET 请求,它们可能会以任何顺序到达。我想要做的是在我将文件附加到 DOM 时保证排序。

如何设置队列系统,以便在触发 A1->A2->A3 时。我可以保证它们按 A1->A2->A3 的顺序附加。

例如,假设 A2 在 A1 之前到达。我希望该操作在 A1 到达和加载时等待。

一个想法是使用定时回调来创建状态检查器

// pseudo-code
function check(ready, fund) {
    // check ready some how
    if (ready) {
        func();
    } else {
        setTimeout(function () {
            check(ready, fund);
        }, 1); // check every msec
    }
}

但这似乎是一种资源繁重的方式,因为我每 1 毫秒触发一次相同的函数,直到加载资源。

这是完成这个问题的正确途径吗?

4

4 回答 4

4

使用 1 毫秒定时回调的状态检查器 - 但这似乎是一种资源繁重的方式;这是完成这个问题的正确途径吗?

不,你应该看看Promises。这样,您可以像这样轻松地制定它:

var a1 = getPromiseForAjaxResult(ressource1url);
var a2 = getPromiseForAjaxResult(ressource2url);
var a3 = getPromiseForAjaxResult(ressource3url);

a1.then(function(res) {
    append(res);
    return a2;
}).then(function(res) {
    append(res);
    return a3;
}).then(append);

例如,jQuery 的.ajax函数实现了这一点。

于 2013-03-26T17:36:48.747 回答
2

你可以尝试这样的事情:

var resourceData = {};
var resourcesLoaded = 0;

function loadResource(resource, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        var state = this.readyState;
        var responseCode = request.status;

        if(state == this.DONE && responseCode == 200) {
            callback(resource, this.responseText);
        }
    };

    xhr.open("get", resource, true);
    xhr.send();
}

//Assuming that resources is an array of path names
function loadResources(resources) {
    for(var i = 0; i < resources.length; i++) {
        loadResource(resources[i], function(resource, responseText) {

            //Store the data of the resource in to the resourceData map,
            //using the resource name as the key. Then increment the
            //resource counter.
            resourceData[resource] = responseText;
            resourcesLoaded++;

            //If the number of resources that we have loaded is equal
            //to the total number of resources, it means that we have
            //all our resources.
            if(resourcesLoaded === resources.length) {
                //Manipulate the data in the order that you desire.
                //Everything you need is inside resourceData, keyed
                //by the resource url. 
                ...
                ...
            }                    
        });
    }
}

如果某些组件必须在其他组件(如某些 JS 文件)之前加载和执行,您可以将 AJAX 请求排队,如下所示:

function loadResource(resource, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        var state = this.readyState;
        var responseCode = request.status;

        if(state == this.DONE && responseCode == 200) {
            //Do whatever you need to do with this.responseText
            ...
            ...

            callback();
        }
    };

    xhr.open("get", resource, true);
    xhr.send();
}

function run() {
    var resources = [
        "path/to/some/resource.html",
        "path/to/some/other/resource.html",
        ...
        "http://example.org/path/to/remote/resource.html"
    ];

    //Function that sequentially loads the resources, so that the next resource 
    //will not be loaded until first one has finished loading. I accomplish
    //this by calling the function itself in the callback to the loadResource 
    //function. This function is not truly recursive since the callback 
    //invocation (even though it is the function itself) is an independent call 
    //and therefore will not be part of the original callstack.
    function load(i) {
        if (i < resources.length) {
            loadResource(resources[i], function () {
                load(++i);
            });
        }
    }
    load(0);
}

这样,在前一个文件完成加载之前,不会加载下一个文件。

如果您不能使用任何第三方库,您可以使用我的解决方案。但是,如果您按照Bergi 的 建议进行操作并使用Promises ,您的生活可能会轻松得多。

于 2013-03-26T17:29:40.050 回答
1

无需check()每毫秒调用一次,只需在 xhr 中运行即可onreadystatechange。如果您提供更多代码,我可以进一步解释。

于 2013-03-26T17:29:48.253 回答
1

我将有一个要执行的函数队列,每个函数都会在执行之前检查先前的结果是否已完成。

var remoteResults[] 

function requestRemoteResouse(index, fetchFunction) {
  // the argument fetchFunction is a function that fetches the remote content
  // once the content is ready it call the passed in function with the result.
  fetchFunction(
    function(result) { 
      // add the remote result to the list of results
      remoteResults[index] = result
      // write as many results as ready.
      writeResultsWhenReady(index);
    });
}

function writeResults(index) {
  var i;
  // Execute all functions at least once
  for(i = 0; i < remoteResults.length; i++) {
    if(!remoteResults[i]) {
      return;
    }
    // Call the function that is the ith result
    // This will modify the dom.
    remoteResults[i]();
    // Blank the result to ensure we don't double execute
    // Store a function so we can do a simple boolean check.
    remoteResults[i] = function(){}; 
  }
}

requestRemoteResouse(0, [Function to fetch the first resouse]);
requestRemoteResouse(1, [Function to fetch the second resouse]);
requestRemoteResouse(2, [Function to fetch the thrid resouse]);

请注意,为简单起见,当前为 O(n^2),如果您在具有 hasRendered 属性的 remoteResults 的每个索引处存储一个对象,它会变得更快但更复杂。然后,您只会向后扫描,直到找到尚未发生的结果或已呈现的结果。

于 2013-03-26T17:51:53.807 回答