首页 > 代码库 > 异步加载图片

异步加载图片

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.clickBox{width:100px;height:20px;border:1px solid red;}
</style>
</head>

<body>

<script type="text/javascript">
var can="http://s1.hao123img.com/resource/site/img/logo.6456fa5.gif";
function D(url){
return new Promise(function(resolve, reject) {
var image = new Image();

image.onload = function() {
resolve(image);
};

image.onerror = function() {
reject(new Error(‘Could not load image at ‘ + url));
};

image.src = http://www.mamicode.com/url;
});
}

D(can).then(function(value) {
alert(‘老二‘);
document.body.appendChild(value);
alert(‘老三‘);
});

alert(‘老大‘);


</script>
</body>
</html>

 

异步加载图片