小程序的运行机制和更新机制

小程序的运行机制

冷启动 与 热启动

冷启动:首次打开和销毁后再次打开

热启动(没有重新启动):已经打开过,5分钟内再次打开,直接切换到前台

前台  后台

前台:小程序运行的时候

后台:(点击离开,或home键离开微信)离开但没有销毁

小程序的销毁

小程序进入后台后,(5分钟内)没有立即销毁,5分钟后可能会被销毁,

如果小程序 占用系统资源过高,持续占用资源,会被系统销毁或被微信的客户端主动的回收

更新机制

检测版本更新

https://developers.weixin.qq.com/miniprogram/dev/api/base/update/UpdateManager.html

运用:

//检测更新
checkUpdate(){
  const updateManager = wx.getUpdateManager()
  //检测版本更新
  updateManager.onCheckForUpdate((res1)=>{
    console.log("onCheckForUpdate.res:", res1) //{hasUpdate: false}
    if (res1.hasUpdate){
      console.log("hasUpate")
      updateManager.onUpdateReady(()=>{
        console.log("onUpdateReady")
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好,是否重启应用?',
          success: function(res){
            console.log("success res:", res)//{errMsg:"showModal:ok",cancel:false,confirm:true}
            if (res.confirm) {
              // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      updateManager.onUpdateFailed(function () {
        // 新版本下载失败
        console.log("onUpdateFailed")
      })
 
    }
  })
}

  

 

猜你喜欢

转载自www.cnblogs.com/baixinL/p/12002257.html