[英]how i can make statement “for” wait in javascript
I have 1 app using phone gap 2.5.0 wrote for ios. 我有一个应用程序使用手机差距2.5.0为ios写道。 I am try to download files in server to my app.
我尝试将服务器中的文件下载到我的应用程序。 I need downloads a lot files (84 files);
我需要下载很多文件(84个文件); and i use 1 loop for to download all.
我用1个循环来下载所有。
But when i use "for" statement, it's loop too fast and download all files almost at the same time, and some file can't download complete because timeout. 但是当我使用“for”语句时,它的循环速度太快并且几乎同时下载所有文件,并且由于超时,某些文件无法下载完成。
So I want 1 file download finish and then the next file begin download. 所以我想要1个文件下载完成,然后下一个文件开始下载。
How can I do that? 我怎样才能做到这一点?
Please help me! 请帮我! I am dumper...
我是自卸车......
This is my code: 这是我的代码:
var fileTransfer = new FileTransfer();
for (var i = 0; i < anhup.length; i++) {
console.log("anhup[" + i + "]: " + anhup[i]);
fileTransfer.download(
"http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
+ anhup[i], window.rootFS.fullPath + "/" + anhup[i],
function(entry) {
sa = entry.fullPath;
console.log("download complete: " + entry.fullPath);
}, function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
});
}
Try 尝试
function download() {
var i = 0;
var fileTransfer = new FileTransfer();
function doDownload(i) {
fileTransfer.download(
"http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
+ anhup[i], window.rootFS.fullPath + "/" + anhup[i],
function(entry) {
sa = entry.fullPath;
console.log("download complete: " + entry.fullPath);
if (i < anhup.length - 1) {
doDownload(i + 1);
}
}, function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
});
}
doDownload(0)
}
Make a function and keep calling itself until the object is empty, like so. 创建一个函数并继续调用自己,直到对象为空,就像这样。
function loadFile() {
// no more to load
if(!anhup.length)
return;
var context = anhup.shift();
fileTransfer.download(url, function(entry) {
console.log("download complete: " + entry.fullPath);
loadFile();
}, function(error) {
// ...
});
}
You can't do that with "for" loop, you need to start downloading the next file when 'success' callback function is called; 你不能用“for”循环来做,你需要在调用'success'回调函数时开始下载下一个文件; the easiest way is to make 'i' global variable, and rewrite your code like this (not tested!):
最简单的方法是制作'i'全局变量,并像这样重写你的代码(未经过测试!):
var i = 0;
var downloadNext;
var onSuccess = function(entry) {
sa = entry.fullPath;
console.log("download complete: " + entry.fullPath);
i = i + 1;
if (i < anhup.length) {
downloadNext();
}
};
var onFail = function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
};
downloadNext = function() {
console.log("anhup[" + i + "]: " + anhup[i]);
fileTransfer.download(
"http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
+ anhup[i], window.rootFS.fullPath + "/" + anhup[i],
onSuccess,
onFail
);
};
// start downloading the first one
downloadNext();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.