浏览器本地缓存localStorage

web 本地存储

var storage={
    save2Local:function (key,val) {
        localStorage.setItem(key,val);
    },
    restore4Local:function(key) {
    if (window.localStorage) {
        return localStorage.getItem(key);
    }
},
/***
 * 清除localStorage
 */
removeLocalStorage:function (key) {
    if(key){
        localStorage.removeItem(key);
    }
}

};

应用:

 var currentDateTime=new Date();
        var date_format='%Y%m%d';
        var currentDate=currentDateTime.format2(date_format);//20170316
        console.log('currentDate:'+currentDate);
        //缓存到浏览器
        var browserCache=function (prefix) {
            var answer=$('textarea[name=content]').val();
            if(answer){//每次currentDate 都需要重新生成
                currentDate=new Date().format2(date_format);//20170316
                storage.save2Local(prefix+currentDate,answer);
            }
        };
        //从浏览器中获取缓存
        var getbrowserCache=function (prefix) {
            var answer=storage.restore4Local(prefix+currentDate);
            if(!answer){
                currentDateTime.setDate(currentDateTime.getDate() - 1);
                currentDate=currentDateTime.format2(date_format);//20170316
                answer=storage.restore4Local(prefix+currentDate);
            }
            answer&&$('textarea[name=content]').val(answer).focus();
        }

猜你喜欢

转载自hw1287789687.iteye.com/blog/2367499