如果你没有选择,只能与这个工作,你可以使用一个集合:
var data; // will contain the retrieved data
/**
* Grab data from the first URL in the list, adding it
* to the global data variable. If the list is empty,
* we got all the data and we succeed. If an XHR fails, we fail.
*/
function prepData(urlList, success, fail) {
if(urlList.length===0) {
succes(); // wrap in timeout if you need to break out of call chain
return;
}
var xhr = new XMLHttpRequest();
var url = urlList.splice(0,1)[0];
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.status === 200 && xhr.readyState === 4) {
data += xhr.responseText;
prepData(urlList, success, fail);
} else {
fail(urlList, xhr); // again, wrap in timeout to break out of call chain
}
}
}
我们可以再用类似的代码调用此:
/**
* prepare our data prior to application "start"
*/
prepData(['...','...','...'], function(){
console.log("finished aggregating data");
// all went well. Start the actual data-consuming code
}, function(notLoadedList, failedXhrObject){
console.log("ohnoes!", notLoadedList, failedXhrObject);
// something went wrong, and we can fail gracefully
});
“从网络服务器下载庞大的多csv文件,并打开它之前加入在客户端的部分用户“几乎是不这样做的原因。如果用户必须下载一个庞大的数据集,那么您的解决方案并不是正确的解决方案。让客户端向服务器询问用户实际可以看到的位,然后编写代码来处理这些问题,而不是需要一个巨大的数据。 – 2013-02-23 16:34:26
我完全同意你的看法。但应用程序已经在那里。今天用户手动合并文件。我只是试图自动化该部分。 – user2102619 2013-02-23 18:11:01