微信小程序如何处理页面缓存

947
2020/12/5 14:50:45
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

微信小程序如何处理页面缓存

微信小程序处理页面缓存的方法:

根据当前时间作为判断依据,缓存数据,代码:

Page({

  data: {

  },

  onLoad: function () {

    //this.getNowTime()

    if(wx.getStorageSync("loaction_time")==this.getNowTime()){

      //读取缓存

       console.log("缓存地址");

        var datevalue=wx.getStorageSync("loaction");

        console.log(datevalue)

    }else{

      wx.removeStorageSync("loaction");

      //请求地址

      console.log("请求地址");

      var loactionname="bs";

      wx.setStorageSync("loaction",loactionname);

      wx.setStorageSync("loaction_time",this.getNowTime());

      var datevalue=wx.getStorageSync("loaction"); 

      console.log(datevalue)

    }

  },getNowTime: function() {

    var now = new Date();

    var year = now.getFullYear();

    var month = now.getMonth() + 1;

    var day = now.getDate();

    if(month < 10) {

      month = '0' + month;

    };

    if(day < 10) {

      day = '0' + day;

    };

    //  如果需要时分秒,就放开

    var h = now.getHours();

    var m = now.getMinutes();

    var s = now.getSeconds();

    //var formatDate = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;

    var formatDate = year + '-' + month + '-' + day;

    //console.log('当前时间',formatDate)

    return formatDate;

  }

})

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 微信小程序二级页面怎么设置导航页