gpt4 book ai didi

javascript - 删除我的本地存储

转载 作者:行者123 更新时间:2023-12-03 07:37:11 28 4
gpt4 key购买 nike

有了以下本地存储脚本,我正在寻找一种方法,以便在 7 天后,如果余额 = 0,余额将被重置。我应该怎么做?

var balance = localStorage.getItem('balance');

if (balance === null) {
// Not found, set value from database or wherever you get the value
balance = 50; // Or whatever value
} else {
// Convert the retrieved value to a number, since
// localStorage stores everything as string.
balance = +balance;
}

function updateBalance(newBalance) {
localStorage.setItem('balance', newBalance);
balance = newBalance;
}

最佳答案

您需要将日期与余额一起存储。由于 localStorage 仅存储字符串,因此您可以使用 JSON 将余额 + 日期对象转换为字符串。像这样的事情:

var balanceObj = {
balance: 0,
transactionDate: new Date()
};

存储它:

localStorage.balance = JSON.stringify( balanceObj );

检索它:

balanceObj = JSON.parse( localStorage.balance );

关于javascript - 删除我的本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35561263/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com