微信小程序處理頁面緩存的方法:
根據當前時間作為判斷依據,緩存數據,代碼:
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;
}
})