封装index.js


async function renderMovie(){
    const res = await fetch('/api/getMovie')
    const json = await res.json()
    let data = json.data.house43
     console.log(data);
    let html = ''
     data.forEach( (item,index) => {  
        if(index < 12){
            html += ' <li> <a href="">'+item.estate_id+'</a>  ===<a href="">'+item.end_time+'</a>  </li> '
        }
      })

     document.querySelector('.ul-box').innerHTML = html
}


async function registerSW(){
    // 1 在网页加载完成的时候 注册 service worker
    window.addEventListener('load', async () => {
        // 2 能力检测
        if ('serviceWorker' in navigator) {
            try{
               await navigator.serviceWorker.register('./sw.js')
            }catch(e){
               console.log('注册失败');
            }
        }
    })
}

renderMovie()
registerSW()


 /**
 *  如果页面一进来, 发现用户没有联网,给用户发一个通知
 * */

  if(Notification.permission === 'default'){
    Notification.requestPermission()
 }

if(!navigator.onLine){
    new Notification('提示',{ body:'你当前没有网络,你访问的是缓存'})
}

// 联网以后的通知
window.addEventListener('online',() => {
    new Notification('提示',{ body:'你已经连上网络了,请刷新访问最新的数据'})
})

 // 断网的通知
 window.addEventListener('offline',() => {
    new Notification('提示',{ body:'网络已断开,你将访问缓存的数据'})
})
posted @ 2022-02-07 15:26  13522679763-任国强  阅读(42)  评论(0)    收藏  举报