p5.js | removeItem() Function

The removeItem() function is used to remove the item that has been stored using the storeItem() function. It removes the value under the given key name from the local storage of the browser.

Syntax:

removeItem(key)

Parameters: This function accept a single parameter as mentioned above and described below.



  • key: This is a string which denotes the key for which the value has to be removed.

Below example illustrates the removeItem() function in p5.js:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(20);
  text("Use the button to set and retrieve random values", 20, 20);
  
  setBtn = createButton('Set items to storage');
  setBtn.position(20, 150);
  setBtn.mouseClicked(setStorage)
    
  getBtn = createButton('Get items from storage');
  getBtn.position(20, 180);
  getBtn.mouseClicked(retrieveStorage)
   
  removeBtn = createButton('Remove string to storage');
  removeBtn.position(20, 210);
  removeBtn.mouseClicked(removeStorage)
  
}
  
function retrieveStorage() {
  clear();
  text("Use the button to set and retrieve random values", 20, 20);
  
  // retrieve values from local storage
  num = getItem('savedNumber');
  bool = getItem('savedBoolean');
  
  // display the values
  text("The retrieved items are:", 20, 50);
  text("Number: " + num, 20, 80);
  text("Boolean: " + bool, 20, 100);
}
  
function setStorage() {
  // generate random values
  randomNum = floor(random(100));
  randomBool = randomNum > 50 ? true : false;
  
  // store values to local storage
  storeItem('savedNumber', randomNum);
  storeItem('savedBoolean', randomBool);
}
  
function removeStorage() {
    
  // remove item from local storage
  removeItem('savedBoolean');
}

chevron_right


Output:
output-gif

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/removeItem



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.