In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to set the validity period and expiration time of localStorage. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Realization idea
Two new methods setCanExpireLocal,getCanExpireLocal are added to the Storage prototype, which are used to set and get values.
It takes at least three times to realize the expiration function, the storage time, the value time, and the effective time.
Access timestamps are generated within the function through Date.now () when the function is called
The valid time is passed in when the value is stored.
If the stored value timestamp plus the valid time is less than the current timestamp indicates that it has not expired, the value is returned
Otherwise, delete the value and return null
The implementation code / / stored value function / / receives three parameters: key, value, valid days Storage.prototype.setCanExpireLocal = (key, value, expire) = > {/ / determine whether the passed validity period is a numeric value or valid / / isNaN is a built-in function of js, which is used to determine whether a value is NaN (non-numeric), / / non-numeric value returns true If the value returns false / /, you can also limit the validity period to an integer, so if (isNaN (expire)) will not be done here | | expire
< 1) { throw new Error('有效期应为一个有效数值') } // 86_400_000一天时间的毫秒数,_是数值分隔符 let time = expire * 86_400_000 let obj = { data: value, //存储值 time: Date.now(), //存值时间戳 expire: time, //过期时间 } // 注意,localStorage不能直接存储对象类型,sessionStorage也一样 // 需要先用JSON.stringify()将其转换成字符串,取值时再通过JSON.parse()转换回来 localStorage.setItem(key, JSON.stringify(obj)) } // 取值函数 // 接收一个参数,存值的键 Storage.prototype.getCanExpireLocal = key=>{let val = localStorage.getItem (key) / / when null if (! val) return val / / is returned directly if there is no value, it is converted to a string. Now val = JSON.parse (val) / / stored value timestamp + valid time = expiration timestamp / / if the current timestamp is greater than the expiration timestamp, it has expired Delete the value and return a prompt if (Date.now () > val.time + val.expire) {localStorage.removeItem (key) return 'value is invalid'} return val.data} / / stored value Storage.prototype.setCanExpireLocal ('test', 'expire in a day' 1) / / value Storage.prototype.getCanExpireLocal ('test') this is the end of the article on "how to set the validity period and expiration time of localStorage". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.