SoFunction
Updated on 2025-04-12

How to clean up front-end fragment localStorage

It may not be well known to everyone.localStorageOn 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.

localStorageIt 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.localStorageThe 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, includinglocalStoragedata.

  • 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 in application logic: In your web application, you can clear it according to specific logiclocalStorage. 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.localStoragedata.

  • Use third-party libraries or tools: Some third-party libraries or tools can help managelocalStorage, including setting expiration time, etc.

Set expiration time

AlthoughlocalStorageIt 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 itemexpiryAttribute, 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 simulatelocalStorageThe 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!