您的位置:首页 > Web前端 > JavaScript

----JS缓存页面数据---

2018-01-02 17:29 176 查看
//简单设置JS缓存
//1. 将数据设置到缓存:
//JsCache.set(key,value,expirs), expirs也可以不设置,默认是60秒
//读取缓存:JsCache._cacheData.key
var JsCache = {
_cacheEndTime: 0, //缓存截止时间
_cacheData: {}, //以JSON对象存储
_currTime: 0, //当前时间戳,是毫秒数

set: function(key,value,timeOut) {
this._currTime = Date.parse(new Date()) / 1000;
timeOut = arguments[2] ? arguments[2] : 60;
this._cacheEndTime = this._currTime + timeOut;
this._cacheData[key] = value;
},

get: function(key) {
this._currTime = Date.parse(new Date()) / 1000;
if (this._cacheEndTime < this._currTime) {  //过期了
this.del(key);
return null;
} else {
return this._cacheData[key];
}
},

del: function(key) {
delete this._cacheData[key];
}
};


可以做页面局部刷新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: