1. 程式人生 > >----JS快取頁面資料---

----JS快取頁面資料---

//簡單設定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];
    }
};


可以做頁面區域性重新整理