2014-12-06 68 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

相关问题