Tag Archives: LocalStorage sets the expiration time.

Localstorage sets the expiration time.

We all know that localStorage does not actively delete and will never be destroyed, so how to set the expiration time of localStorage?Let’s try it together today!
<script type=”text/javascript”>
function set(key,value){
var curTime = new Date().gettime ();
localStorage. SetItem (key, JSON stringify ({data: value, time: curTime}));
}
function get(key,exp){
var data = localStorage. GetItem (key);
var dataObj = json.parse (data);
if (new Date(). GetTime () – dataobj. time> Exp) {
console.log(‘ message has expired ‘);
// alert (” overdue information “)
} else {
// console log (” data = “+ dataObj. Data);
// the console. The log (JSON. Parse (dataObj. Data));
var dataObjDatatoJson = json.parse (dataobj.data)
return dataObjDatatoJson;}}
</script>

usage scenario:
1. Use local data to reduce network transmission
2. Weak network environment, high latency, low bandwidth, try to localize the data
Usage:
< script>
window.onload = function(){
var Ipt = document.getElementById(‘input1’);
var value = ‘{” name “:” and send the bore is clear “, “Age” : “18”, “address” : “lujiazui city”} “;
set (‘ information, value);
Function (){
//var dataObjData=get(‘information’,1000);
//var dataObjData=get(‘information’,1000*60);
//var dataObjData=get(‘information’,1000*60*60);
// expiration time is 1 hour// var Obj = get (” information “, 1000 * 60 * 60 * 24);
var dataObjData=get(‘information’,1000*60* 24*7);
console.log(dataObjData || null);
if (dataObjData! =”” & & dataObjData! . = null) {
the console log (” name: “+ dataObjData. Name);
the console. The log (” Age: “+ dataObjData. Age);
the console. The log (” address: “+ dataObjData. Age);
} else {
alert (” access to information has expired “);
}
}
}
< /script>

1 function setLocalStorage(key, value) {
 2     var curtime = new Date().getTime(); // Get the current time and convert it to a JSON string sequence. 
 3     var valueDate = JSON.stringify({
 4         val: value,
 5         timer: curtime
 6     });
 7     try {
 8         localStorage.setItem(key, valueDate);
 9     } catch(e) {
10         /*if(e.name === 'QUOTA_EXCEEDED_ERR' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
11             console.log("Error: Local Storage Exceeds Limit");
12             localStorage.clear();
13         } else {
14             console.log("Error: Failure to save to local storage");
15         }*/
16         // Compatibility Writing
17         if(isQuotaExceeded(e)) {
18             console.log("Error: Local Storage Exceeds Limit");
19             localStorage.clear();
20         } else {
21             console.log("Error: Failure to save to local storage");
22         }
23     }
24 }
25 
26 function isQuotaExceeded(e) {
27     var quotaExceeded = false;
28     if(e) {
29         if(e.code) {
30             switch(e.code) {
31                 case 22:
32                     quotaExceeded = true;
33                     break;
34                 case 1014: // Firefox 
35                     if(e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
36                         quotaExceeded = true;
37                     }
38                     break;
39             }
40         } else if(e.number === -2147024882) { // IE8 
41             quotaExceeded = true;
42         }
43     }
44     return quotaExceeded;
45 }
46 
47 function getLocalStorage(key) {
48     var exp = 60 * 60 * 24; // Seconds of the day
49     if(localStorage.getItem(key)) {
50         var vals = localStorage.getItem(key); // Get locally stored values 
51         var dataObj = JSON.parse(vals); // Converting Strings to JSON Objects
52         // If (current time - the time the stored element was set at creation) > Expiration time 
53         var isTimed = (new Date().getTime() - dataObj.timer) > exp;
54         if(isTimed) {
55             console.log("Storage has expired");
56             localStorage.removeItem(key);
57             return null;
58         } else {
59             var newValue = dataObj.val;
60         }
61         return newValue;
62     } else {
63         return null;
64     }
65 }
66 
67 setLocalStorage('name', 'hello, world');
68 var localKey = getLocalStorage('name');
69 console.log(localKey);