<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);
Read More:
- Javascript SecurityError: Failed to read the’localStorage’ property from’Window’: Access is denied for this document.
- Interface request error 504 gateway time out [How to Solve]
- How to Set Time Zone in Golang
- [Solved] React Startup Error at the First time :SyntaxError: Unexpected token
- Golang: How to Read File All Content in one time
- Vue a page is mounted to send multiple requests at the same time, and the loading is processed uniformly
- Javascript: Simple package localStorge operation
- [Solved] QuotaExceededError the quota has been exceeded — Firefox
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- @requestbody: How to Use or Not Use
- [Solved] ajax Error: Uncaught SyntaxError: Unexpected end of JSON input
- [Solved] Vue Element Date plug-in reports an error in form validation
- [Solved] JS Error: Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML‘)
- The date selector Report Null Error in element is cleared
- Error in created hook: “SyntaxError: Unexpected token u in JSON at position 0
- Solve the asynchronous execution of callback function in Axios request processing interceptor
- Json.parse: All Error & How to Solve Them
- [Solved] TypeError: Converting circular structure to JSON – JSON.stringify
- JS uses onerror to automatically catch exceptions
- [Solved] JSON.parse() Error: Unexpected end of JSON input