图片异步加载
1.计时器异步加载(缺点很明显,不知道图片加载时长,受网络影响)
$(function(){
setTimeout(function(){
console.log(document.getElementById("image").complete)
// function
},1000)
})
<img src="./long.png" id="image" onload="handleLoad()">
function handleLoad () {
console.log("完成")
console.log(document.getElementById("image").height)
// function
}
$(function(){
loadImageAsync()
})
function loadImageAsync () {
return new Promise(function(resolve, reject) {
let image = document.getElementById("image")
image.onload = function(){
console.log("完成")
console.log(document.getElementById("image").height)
// function
resolve(image)
}
image.onerror = function() {
reject(new Error('Could not load image'));
}
})
}