2013-04-23 89 views
0

我有1个应用程序使用手机差距2.5.0写为ios。 我尝试将服务器中的文件下载到我的应用程序。我需要下载很多文件(84个文件);我使用1循环来下载所有。我怎么能说声“等”在javascript

但是,当我使用“for”语句时,它的循环速度太快,几乎同时下载所有文件,而某些文件由于超时而无法下载完成。

所以我想要1个文件下载完成,然后下一个文件开始下载。

我该怎么做?

请帮帮我!我自卸车...

这是我的代码:

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); 
     }); 
} 

回答

1

尝试

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) 
} 
+0

谢谢,这是工作。 :) – 2013-04-23 03:47:36

+0

请解释。 :) – 2014-09-12 11:46:00

1

做一个功能,并保持自称,直到对象是空的,就像这样。

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) { 
     // ... 
    }); 
} 
+0

anhup.splice(0,1)可能是anhup.shift() – bfavaretto 2013-04-23 03:27:24

+0

感谢您的提示,我认为这样的考虑'.pop()'存在的东西。 – 2013-04-23 03:29:29

+0

谢谢!我会尝试待办事项! :) – 2013-04-23 03:48:16

0

你不能用“for”循环做到这一点,你需要在'success'回调函数被调用时开始下载下一个文件;最简单的方法是使'我'全局变量,并重写你的代码(未测试!):

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(); 
+0

谢谢!我会测试! – 2013-04-23 03:49:13