Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the expiration time of localStorage in html5

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to set the expiration time of localStorage in html5". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

/ / encapsulate the expiration control code 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 ('information expired'); / / alert ('information expired')} else {/ / console.log ("data=" + dataObj.data); / / console.log (JSON.parse (dataObj.data)) Var dataObjDatatoJson = JSON.parse (dataObj.data) return dataObjDatatoJson;}}

Use the scene:

1. Reduce network transmission by using local data

two。 In a weak network environment, high latency, low bandwidth, try to localize data

How to use it:

_ window.onload = function () {var Ipt = document.getElementById ('input1'); var value =' {"name": "and Pai Kongming", "Age": "18", "address": "Lujiazui City"}'; set ('information',value); Ipt.onclick = function () {/ / var dataObjData=get (' information',1000) / / the expiration time is 1 second. Normally, the expiration time is / / var dataObjData=get ('information',1000*60); / / the expiration time is 1 minute / / var dataObjData=get (' information',1000*60*60); / / the expiration time is 1 hour / / var Obj=get ('information',1000*60*60*24) / / Expiration time is 24 hours var dataObjData=get ('information',1000*60*60*24*7); / / Expiration time is 1 week console.log (dataObjData | | null); if (dataObjDatadatabases = "" & dataObjDataexpiry null) {console.log ("name:" + dataObjData.name); console.log ("Age:" + dataObjData.Age) Console.log ("address:" + dataObjData.Age);} else {alert ("the information obtained has expired");} "how to set the expiration time of localStorage in html5" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report