It may not be well known to everyone.localStorage
On the front end is a favorable == "solid state tool" ==.
Maintain data status and do not refresh with page refresh, which means it is separated from the normal life cycle. Using this feature can complete many shocking operations
However, if you want the data state to be restored to the initial state after the project test, you need to understand the clearance of localStorage.
localStorage
It is part of web storage, which allows websites to store data on the user's browser, and the data can still be retained even after the browser is closed.localStorage
The data in has no expiration time, so the data is permanently saved until it is explicitly cleared.
To clear the data in localStorage, you can take the following methods:
Manually clear: Users can clear browsing data in the browser settings, including
localStorage
data.-
Programmatically clear:
- Clear specific items:
('key'); // Delete the data item with the key 'key'
- Clear all data:
(); // All data in `localStorage` will be deleted
- Clear specific items:
-
Clear in application logic: In your web application, you can clear it according to specific logic
localStorage
. For example, when a user logs out, you may want to clear saved user information.function logout() { // Clear user-related localStorage data ('userToken'); // Redirect to login page or other page = '/login'; }
Use the browser-provided clearing function: Most modern browsers provide the option to clear browsing data, and users can choose to clear in browser settings.
localStorage
data.Use third-party libraries or tools: Some third-party libraries or tools can help manage
localStorage
, including setting expiration time, etc.
Set expiration time
AlthoughlocalStorage
It does not support setting expiration time itself, but you can implement similar functions in the application logic:
function setItemWithExpiry(key, value, expiry) { const item = { value: value, expiry: expiry }; (key, (item)); } function getItemWithExpiry(key) { const itemStr = (key); if (!itemStr) return null; const item = (itemStr); const currentTime = new Date().getTime(); if (currentTime > ) { (key); return null; } return ; } // Set an item with an expiration timeconst now = new Date().getTime(); setItemWithExpiry('myKey', 'myValue', now + 3600000); // Expired in 1 hour // Get an item with an expiration timeconst value = getItemWithExpiry('myKey');
In this example, we add a for each stored itemexpiry
Attribute, indicating the expiration time of this item. When getting data, we check whether the current time has exceeded the expiration time, and if so, clear the item.
In this way, you can simulatelocalStorage
The expiration time function makes the data automatically invalid after a certain period of time.
Summarize
This is the article about the cleaning of front-end fragment localStorage. For more related content on front-end localStorage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!