在$ .each中使用回调函数然后循环不等待函数完成

时间:2017-05-30 05:19:02

标签: javascript jquery

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>';

            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 -->

当我在$ .each中使用回调时,循环不能同步.Iterate不等待功能完成。任何人都可以帮助我。

我们将尽一切努力

谢谢!

1 个答案:

答案 0 :(得分:0)

您已将功能定义为module1module2,但在modulesDemo数组中,您将名称设为:

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

这些名称与实际功能名称不同(请注意下划线?)。所以你的功能不会被调用。

编辑: 根据您的评论,您的问题无法复制。请参阅下面的代码段。

&#13;
&#13;
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);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id='demoCanvas'></canvas>
&#13;
&#13;
&#13;