网站未加载时页面上的指示

时间:2018-06-23 11:43:16

标签: javascript html timeout runtime-error loading

当服务foursquare API未加载(无法加载)时,如何在页面上显示可见的指示?对于以下代码;

 const fourSquare = PlacesAPI.getFoursquareData();

 PlacesAPI.getPlaces().then(places => {

 for (let i = 0; i < places.length; i++) {

    let apiUrl = fourSquare.baseUrl + places[i].foursquareID + fourSquare.picSuffix;

    PlacesAPI.getFoursquarePicture(apiUrl).then(pictureUrl => {

      places[i].picture = pictureUrl;
    }).catch(error => {
      console.error(error);
    })
  }

1 个答案:

答案 0 :(得分:0)

您可以在页面上添加一个“ spinner”,然后在加载完成后将其隐藏。 (随附的微调器来自spinkit

const spinner = document.getElementById('spinner');
new Promise((resolve) => {
    console.log('Started loading...');
    
    // Simulate work...
    setTimeout(resolve, 3000);
  }
).then(() => {
  console.log('Finished loading!');

  // Hide the spinner
  spinner.style.display = 'none'; 
})
.spinner {
  margin: 100px auto;
  width: 50px;
  height: 40px;
  text-align: center;
  font-size: 10px;
}

.spinner > div {
  background-color: #333;
  height: 100%;
  width: 6px;
  display: inline-block;
  
  -webkit-animation: sk-stretchdelay 1.2s infinite ease-in-out;
  animation: sk-stretchdelay 1.2s infinite ease-in-out;
}

.spinner .rect2 {
  -webkit-animation-delay: -1.1s;
  animation-delay: -1.1s;
}

.spinner .rect3 {
  -webkit-animation-delay: -1.0s;
  animation-delay: -1.0s;
}

.spinner .rect4 {
  -webkit-animation-delay: -0.9s;
  animation-delay: -0.9s;
}

.spinner .rect5 {
  -webkit-animation-delay: -0.8s;
  animation-delay: -0.8s;
}

@-webkit-keyframes sk-stretchdelay {
  0%, 40%, 100% { -webkit-transform: scaleY(0.4) }  
  20% { -webkit-transform: scaleY(1.0) }
}

@keyframes sk-stretchdelay {
  0%, 40%, 100% { 
    transform: scaleY(0.4);
    -webkit-transform: scaleY(0.4);
  }  20% { 
    transform: scaleY(1.0);
    -webkit-transform: scaleY(1.0);
  }
}
<div class="spinner" id="spinner">
  <div class="rect1"></div>
  <div class="rect2"></div>
  <div class="rect3"></div>
  <div class="rect4"></div>
  <div class="rect5"></div>
</div>