【问题标题】:How to properly make a preloader a page如何正确地使预加载器成为页面
【发布时间】:2021-05-20 16:00:20
【问题描述】:

我有一个在加载时执行一些数据提取的网站。我正在尝试为此网站制作一个预加载器,该加载器仅在网站完全加载和完成任何初始请求所需的时间后才会显示。

我看过一些关于预加载器的教程,他们只是在加载器上设置了一个时间间隔,但我试图让加载器真正等待页面加载。

有什么建议吗?

【问题讨论】:

    标签: javascript html css node.js loader


    【解决方案1】:

    你可以使用async函数

    function resolveAfter2Seconds() {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve('resolved');
        }, 2000);
      });
    }
    
    async function asyncCall() {
      console.log('calling');
      const result = await resolveAfter2Seconds();
      console.log(result);
      // expected output: "resolved"
    }
    
    window.addEventListener('load', async (event) => {
      let loading = true 
      // this loading value helps you to know when to show your loader
    
      await console.log('page is loading');
      await asyncCall()
    
      loading = false
      console.log('page is loaded');
    });

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 2016-12-28
      • 1970-01-01
      • 2020-05-23
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多