美文网首页
localStorage的简单封装

localStorage的简单封装

作者: DCbryant | 来源:发表于2017-11-06 13:56 被阅读54次
    const Storage = (function(){
        return{
            set:function(key,value,expireSeconds){
                localStorage[key] = JSON.stringify({
                    value,
                    expired:expireSeconds === undefined ? undefined :Date.now() + 1000*expireSeconds
                })
            },
            get:function(key){
                if(localStorage[key] === undefined){
                    return
                }
                let object = JSON.parse(localStorage[key])
                if(object.expired === undefined || Date.now() < object.expired){
                    return object.value
                }else{
                    delete localStorage[key]
                }
            }
        }
    })()
    
    

    相关文章

      网友评论

          本文标题:localStorage的简单封装

          本文链接:https://www.haomeiwen.com/subject/nkllmxtx.html