我的情况是我需要使用javascript检查图像的大小,然后根据其宽度执行一段代码。这是我的代码
var width, height;
var img = new Image();
img.src = "pool.jpg";
img.onload = function () {
width = this.width;
height = this.height;
console.log(width);
}
console.log(width);
if (width > 0) {
console.log('in condition check');
//Some code to be executed
}
//Some other code to be executed which cannot be put in the onload portion
问题是该img.onload
部分仅在以下代码完成执行后才起作用。有没有办法触发该img.onload
功能并以同步方式执行代码。