该代码检查是否存在图像。检查图像是否存在,变量值不变
函数onload内部我想用布尔值更改变量'control'。
var control = 'a.jpg';
var image_name = control;
var image = new Image();
image.onload = function() {
control = true; // Why this change does not happen outside of this function?
};
image.onerror = function() {
control = false; // Why this change does not happen outside of this function?
};
image.src = image_name;
console.log(control); // The value is not changed in a boolean
但是在函数之外,变量没有改变。为什么?
感谢