如何在脚本中等待上传完成后再继续上传下一个文件?
我搜索了互联网,但只有setTimeout功能,等等! 我的代码在这里:
function post(id){
var formdata = new FormData();
var id = f+1;
var file = _("filestyle-"+id).files[0];
formdata.append("file"+id, file);
var title = _("title"+id).value;
formdata.append("title"+id, title);
document.getElementById("uploadname").innerHTML=title+"is uploading<br>";
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "../swupload.php");
ajax.send(formdata);
}
在ajax.send(formdata);
之后添加此代码:
ajax.onreadystatechange = function() {
if (ajax.readyState==4 && ajax.status==200 && id < 4) {
post(id + 1);
}
}
不显示completHandler中的.target.responseText
completeHandler:
function completeHandler(event){
_("progressBar").innerHTML = event.target.responseText;
}
答案 0 :(得分:2)
您可以将您想要在ajax请求的不同阶段执行的代码设置为: -
readyState保存XMLHttpRequest的状态。从0到4的变化:
0:请求未初始化
1:建立服务器连接
2:收到请求
3:处理请求
4:请求完成且响应准备就绪
但是,在您的情况下,您希望在完成上传后执行代码,可能会达到以下目的:
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && ajax.status==200)
{
// code to execute after upload
}
}
答案 1 :(得分:1)
Ajax是异步的,因此您不能指望其他代码等待它。你可以做的是用一个函数替换你的循环,并从你的 @Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.splash, container, false);
//Initialize facebook
FacebookSdk.sdkInitialize(getApplicationContext());
loginButton = (Button) view.findViewById(R.id.login_button);
loginButton.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
//Login with facebook on clicking custom button
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
});
//Register a callback
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// login successful
}
@Override
public void onCancel() {
// login cancelled
}
@Override
public void onError(FacebookException exception) {
// login error
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
回调中调用它。
completeHandler
如果必须在此函数范围之外定义post(1);
function post(id) {
var formdata = new FormData();
var file = _("filestyle-"+id).files[0];
formdata.append("file"+id, file);
var title = _("title"+id).value;
formdata.append("title"+id, title);
document.getElementById("uploadname").innerHTML=title+"is uploading<br>";
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "../swupload.php");
ajax.send(formdata);
function completeHandler() {
if (id < 5)
post(id + 1);
}
}
,请保留id,如下所示:
completeHandler
ajax.id = id;