gpt4 book ai didi

javascript - 当您调用clear()然后在同一函数中设置一些本地存储值时不清除本地存储

转载 作者:行者123 更新时间:2023-12-03 00:10:03 26 4
gpt4 key购买 nike

having some data in localStorage, in my function first clearing the localstorage using clear() method and then adding one value to the localstorage. But its not clearing the old data

function() {
localStorage.clear();
localStorage.setItem('test', 'test');
}

最佳答案

你调用你的函数还是应该是一个自执行函数?

自执行示例:

(function(){
localStorage.clear();
localStorage.setItem('test', 'test');
})();

或作为箭头函数 (es6)

(() => {
localStorage.clear();
localStorage.setItem('test', 'test');
})()

关于javascript - 当您调用clear()然后在同一函数中设置一些本地存储值时不清除本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54780867/

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