奥利给
Page({ /** * 页面的初始数据 */ data: { ListData: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { var that = this; var timestamp = Date.parse(new Date()); //时间戳 var expiration = timestamp + 1800000; //缓存半个小时 var key_data = wx.getStorageSync("keydata"); //获取异步缓存数据 var data_expiration = wx.getStorageSync("data_expiration"); if (data_expiration && key_data) { if (timestamp > data_expiration) { wx.hideLoading(); //删除缓存再次请求 wx.clearStorageSync(); wx.clearStorage(); wx.setStorageSync("data_expiration", expiration); that.Getapi(); }else{ //获取缓存数据 that.setData({ ListData: key_data }) } } else { wx.hideLoading(); //请求 wx.setStorageSync("data_expiration", expiration); that.Getapi(); } }, /** * 请求 */ Getapi: function() { var that = this; wx.showLoading({ title: '玩命加载中', mask: true }); wx.request({ url: "接口", data: {}, method: 'GET', dataType: 'json', success(result) { wx.hideLoading(); //将请求成功的数据异步存储到本地 wx.setStorage({ key: 'keydata', data: result.data }) that.setData({ ListData: result.data }) } }) } })