我们来实现一个图片的加载;设置第一张图片加载1s之后加载第二张图片:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Promise的应用-异步加载</title>
<style>
img{
width: 300px;
padding: 10px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
const loadImgAsync = (url)=> {
return new Promise((resolve,reject) =>{
const img = new Image();
img.onload = () => {
setInterval(() => {
resolve(img);
}, 1000);