2014-12-06 71 views
0

我使用PhoneGap插件中的FileTransfer.download函數來下載我應用程序中的文件。Javascript - 在asyncronus函數完成後,下一行如何等待執行?

該函數是assyncronus,所以下一行執行時不依賴於此函數的結論。

我的應用程序應用程序在其登錄過程中從服務器捕獲用戶照片,因此在此操作結束後用戶無法移動到主屏幕。但用我的實際代碼,它不會發生,因爲我不知道該怎麼做。

下面你可以檢查我的代碼:

var usuarios = json.usuarios; 

var fileTransfer = new FileTransfer(); 

for(var key in usuarios) { 
    if(usuarios.hasOwnProperty(key)){ 
     if(usuarios[key].tipo == "titular"){ 

      var titular = { 
       "id"    : usuarios[key].id, 
       "email"    : $("#login-email").val(), 
       "foto"    : "images/perfil.jpg" 
      }; 

      localStorage.setItem('appnowa-titular', JSON.stringify(titular)); 

      if(usuarios[key].foto == "1"){ 

       window.fileDownloadName = "appnowa-perfil-" + usuarios[key].id + ".jpg"; 

       console.log('downloadFile'); 

       window.requestFileSystem(
        LocalFileSystem.PERSISTENT, 
        0, 
        function(fileSystem){ 
         console.log('onRequestFileSystemSuccess'); 
         fileSystem.root.getFile(
          'dummy.html', 
          {create: true, exclusive: false}, 
          function(fileEntry){ 
           console.log('onGetFileSuccess!'); 
           var path = fileEntry.toURL().replace('dummy.html', ''); 
           var fileTransfer = new FileTransfer(); 
           fileEntry.remove(); 

           fileTransfer.download(
            'https://www.myserver.com/imagens/clientes/' + window.fileDownloadName, 
            path + window.fileDownloadName, 
            function(file) { 
             console.log('download complete: ' + file.toURL());me; 
             titular.foto = file.toURL(); 
            }, 
            function(error) { 
             console.log('download error source ' + error.source); 
             console.log('download error target ' + error.target); 
             console.log('upload error code: ' + error.code); 
             titular.foto = "images/perfil.jpg"; 
            } 
           ); 
          }, 
          fail 
         ); 
        }, 
        fail 
       ); 

       console.log("1 " + titular.foto); 
      } 
     } 
    } 
} 

localStorage.setItem('appnowa-dependentes', JSON.stringify(dependentes)); 

appNowa.pushPage('mainPage.html'); //go to next page 
+3

將需要異步調用與結束相關的回調函數發生後,代碼即異步處理。 – nnnnnn 2014-12-06 03:34:44

+0

我認爲這是行不通的,因爲這段代碼被執行超過1次(取決於與記錄用戶相關的用戶數 - 依賴者)。例如,我有10個依賴用戶,所以這個代碼將被執行10次以捕獲他們的照片。 – 2014-12-06 03:38:44

+2

只有你可以使用異步回調的結果的地方是回調本身。 Javascript不會像你想要的那樣等待執行。你必須設計異步行爲。 – jfriend00 2014-12-06 04:31:41

回答

1

我想你可以通過兩種方式實現它,

在所有的回調的狀態測試鏈接的回調或;

我會寫的僞代碼,我認爲可能是一個可行的解決方案(未測試,無擔保)

afterCharge=function(){ 
    doYourStuffHere; 
} 

//method 1 in your callback 
var complete=0 
len = usuarios.lenght; 
for(var key in usuarios){ 
    //do yourstuff 
    fileTransfer.download(
     file, 
     path, 
     function(file){ 
     //do your stuff 
     complete++; 
     if(len===complete){ 
      afterCharge(); 
     } 
     }, 
     function(error){ 
     //do your stuff 
     complete++; 
     if(len===complete){ 
      afterCharge(); 
     } 
     } 
); 
} 
+0

太棒了!非常感謝!它與您的解決方案一起工作! – 2014-12-06 11:24:43

相關問題