2013-04-23 59 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