該代碼檢查是否存在圖像。檢查圖像是否存在,變量值不變
函數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
但是在函數之外,變量沒有改變。爲什麼?
感謝