OnLoad用于图像回调以防止" undefined"

时间:2016-09-21 16:31:33

标签: javascript jquery

嗯,在这种情况下如何使OnLoad异步?

否则我必须在OnLoad闭包中输入许多代码。

$.each(data, function() {
  albumPhoto = 'http://assets.example.com/' + this.photo;

  var temp_img = new Image(),
    albumPhotoWidth, albumPhotoHeight

  temp_img.src = albumPhoto;
  temp_img.onload = function() {
    console.log(this.naturalWidth) // have something
    albumPhotoWidth = this.naturalWidth;
    albumPhotoHeight = this.naturalHeight;
  }

  console.log(albumPhotoWidth) //undefined ?? I want to use it here

});

0 个答案:

没有答案