我有这个函数在我调用函数时将图像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在退出匿名函数后会丢失数据。如何在函数后访问该值?