从匿名函数中获取价值

时间:2015-10-12 12:25:33

标签: javascript jquery

我有这个函数在我调用函数时将图像url转换为base64字符串我丢失了globalvar数据。

脚本:

    function convertImgToBase64(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this,0,0);
        var dataURL = canvas.toDataURL(outputFormat || 'image/png');
        callback(dataURL);
        canvas = null;
    };
    img.src = url;
}

并致电

        convertImgToBase64(url, function(base64Img){
            //console.log(imageData);
            globalvar = base64Img;
        });

但是globalvar在退出匿名函数后会丢失数据。如何在函数后访问该值?

0 个答案:

没有答案