我有这样的代码:
var links="";
function upload(file){
var fd = new FormData();
fd.append("image", file); // Append the file
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://someapi.com");
xhr.onload = function() {
// Big win!
// The URL of the image is:
links += "[IMG]"+JSON.parse(xhr.responseText).upload.links.original+"[/IMG]\n";
//showResult(JSON.parse(xhr.responseText).upload.link);
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
}
function catchFiles(files){
nfiles = files.length;
//var links="";
for(var i=0;i<nfiles;i++){
upload(files[i]);
}
}
function showResult(links){
document.getElementById('div_result').innerText = links;
}
我想要的是如何等到 catchFiles 完成(上传完成的所有文件)之后,将调用 showResult。
有什么解决办法吗?
谢谢