当我试图在两个函数之间传递值时,我得到了错误消息这里有一个错误";未捕获引用错误:mahi未定义";我已经定义了它,并在第121行运行,但仍面临错误
function encode() {
var selectedfile = document.getElementById("cimg").files;
if (selectedfile.length > 0) {
var imageFile = selectedfile[0];
var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result;
window.mahi = srcData; // here i have been defined variable as mahi
console.log(mahi) // here is variable work
}
fileReader.readAsDataURL(imageFile);
}
}
console.log(mahi) //here is a error "Uncaught ReferenceError: mahi is not defined" i have it defined and asigned at line no 121 stilli am facing error
因为在encode()
外部调用console.log(mahi)
时,变量尚未声明。