'How to change localStorage object-variable to false after a certain amount of time

I have an issue with a project where i need to change an object variable to false and save it to local storage.

let animalHtml = // Checking if animal is fed or not.
!extendedAnimalInfo.isFed || caluclateFeeding() ? (
  <StyledButton
    key={extendedAnimalInfo.id}
    onClick={() => {
      handleClick();
    }}
  >
    Feed me!
  </StyledButton>
) : (
  <div key={extendedAnimalInfo.id}>
    <p>I Am fed!</p>
    <p>
      You fed me at:{" "}
      <StyledButton>{extendedAnimalInfo.lastFed}</StyledButton>
      Come back and feed me in "X" time
    </p>
  </div>
);

setting animal.isFed to true after button click.

function handleClick() {
let animalFromLS: IExtendedAnimal[] = JSON.parse(
  localStorage.getItem("Animals") || "[]"
);
animalFromLS.map((animal, i) => {
  if (params.id == animal.id.toString()) {
    animalFromLS[i] = {
      ...extendedAnimalInfo,
      isFed: true,
      lastFed: timestamp,
    };
  }
});
localStorage.setItem("Animals", JSON.stringify(animalFromLS));
setExtendedAnimalInfo({
  ...extendedAnimalInfo,
  isFed: true,
  lastFed: timestamp,
});

}

And then i look and see if X time has elapsed to be able to feed again.

function caluclateFeeding() {
let dateParse = Date.parse(Date());
let total = dateParse - Date.parse(extendedAnimalInfo.lastFed);

if (dateParse - Date.parse(extendedAnimalInfo.lastFed) > 10000) {
  console.log(total);
  return true;
}
return false;

}

But it never returns false and i dont know where to save it to localStorage.

Please help someone.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source