2017-05-30 38 views
-4
var modulesDemo = ['module1', 'module2']; 
var moduleHtml='', pdfContent=''; 

$。每个(modulesDemo,函数(指数,模块){使用回调函数,然后循环不会等待功能齐全

if(typeof window[module] === 'function'){ 

    window[module](function(content){ 
     moduleHtml +=content; 

     if(modulesDemo.length==index+1){ 
      pdfContent += '<table>'; 
      pdfContent += moduleHtml; 
      pdfContent += '</table>'; 

      console.log(pdfContent); 
     } 
    }); 

} 

});

function module1(callback){ 
    var content=''; 
    var canvas = $("#demoCanvas")[0]; 
    var img = canvas.toDataURL("image/png"); 
    content += '<tr>'; 
    content += '<td>'; 
    content += '<img style="width:100%;height:100%;margin-top:20px !important" src="'+img+'" />'; 
    content += '</td>'; 
    content += '</tr>'; 
    callback(content); 
} 

function module2(callback){ 
    function module2(callback){ 
    var img; 
    var content; 
    html2canvas($("#demoCanvas1"), { 
     onrendered: function(canvas) { 
      img = canvas.toDataURL("image/png"); 
      content += '<tr>'; 
      content += '<td>'; 
      content += '<img style="width:100%;height:100%;margin-top:20px !important" src="'+img+'" />'; 
      content += '<td>'; 
      content += '</tr>'; 
      //alert(2); 
      callback(content); 
     } 
    }); 
} 
} 

<!-- language: lang-html --> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <canvas id='demoCanvas'></canvas> 

<!-- end snippet --> 

,当我使用$。每次回调然后循环不工作synchronous.Iterate不会等待功能complete.Can任何人帮助我。

将尽一切努力理解

谢谢!

+0

在这里每个$,你已经注册同步回调,但以什么顺序,他们将被称为不保证。 –

+0

那么问题的症状是什么,moduleHtml的顺序是错误的? – James

回答

0

您已经定义了功能module1module2modulesDemo数组你把名字为:(?注意下划线)

var modulesDemo = ["module_1","module_2"]; 

这些名字不是实际的功能名称不同。所以你的功能没有被调用。

编辑: 根据您的评论,您的问题不能被复制。请参阅下面的代码片段。

var modulesDemo = ['module1', 'module2']; 
 
var moduleHtml='', pdfContent=''; 
 
$.each(modulesDemo, function(index, module) { 
 

 
    if(typeof window[module] === 'function'){ 
 

 
     window[module](function(content){ 
 
      moduleHtml +=content; 
 

 
      if(modulesDemo.length==index+1){ 
 
       pdfContent += '<table>'; 
 
       pdfContent += moduleHtml; 
 
       pdfContent += '</table>'; 
 
       //$scope.exportDashboardProcess(pdfContent,layout,size); 
 
       console.log(pdfContent); 
 
      } 
 
     }); 
 

 
    } 
 

 

 

 
}); 
 

 
function module1(callback){ 
 
    var content=''; 
 
    var canvas = $("#demoCanvas")[0]; 
 
    var img = canvas.toDataURL("image/png"); 
 
    content += '<tr>'; 
 
    content += '<td>'; 
 
    content += '<img style="width:100%;height:100%;margin-top:20px !important" src="'+img+'" />'; 
 
    content += '</td>'; 
 
    content += '</tr>'; 
 
    callback(content); 
 
} 
 

 
function module2(callback){ 
 
    var content=''; 
 

 
    var canvas = $("#demoCanvas")[0]; 
 
    var img = canvas.toDataURL("image/png"); 
 
    content += '<tr>'; 
 
    content += '<td>'; 
 
    content += '<img style="width:100%;height:100%;margin-top:20px !important" src="'+img+'" />'; 
 
    content += '</td>'; 
 
    content += '</tr>'; 
 
    callback(content); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<canvas id='demoCanvas'></canvas>

+0

对不起,我纠正了。 var modulesDemo = [“module1”,“module2”]; 对不起,我很急的写下了 – Yash

+0

@yashkumar如果那是一个输入错误,那么这个问题就不能被复制。 –

-1

exportDashboard才可以使用的setTimeout function.May是这将帮助你