- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我使用 localStorage.removeItem("key");
删除一个项目,有什么方法可以检查删除是否成功?回调或 promise 之类的东西?
现在我是这样做的:
if(localStorage.getItem("key"))
localStorage.removeItem("key");
else
console.log("Error");
这是正确的方法还是可以用“更好”的方法来完成?
最佳答案
removeItem()
调用不会返回任何任何 类型的失败指示1(摘自 a recent editor's draft of the W3C web storage specification,我的重点):
The
removeItem(key)
method must cause the key/value pair with the given key to be removed from the list associated with the object, if it exists. If no item with that key exists, the method must do nothing.
因此,判断 key 是否被实际删除(与上面第二句中的“什么都不做”操作相反)的唯一方法是先检查它。
您几乎可以肯定地使用 getItem()
返回值针对 null
的显式检查(当然使用 ===
)但是不会改变您无法通过 removeItem()
本身1 检测到故障的事实。
当然,如果您担心这些片段会干扰您的代码,您完全可以定义一个函数来为您完成繁重的工作,例如:
function removeExistingItem(key) {
if (localStorage.getItem(key) === null)
return false;
localStorage.removeItem(key);
return true;
}
然后用更简洁的方式调用它:
if (! removeExistingItem("key"))
console.log("Error");
1 这是基于“失败”被定义为 key 不存在(这似乎是您在问题中使用的定义)。实际上,removeItem()
不能 失败仅仅是因为它在项目不存在的情况下什么都不做(见上文插图)。
关于javascript - 检查 localStorage.removeItem() 是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31448160/
为什么 removeItem 不需要括号? 为什么 li.onclick = removeItem 必须在 newItem() 函数内? li.onclick 事件发生时的逻辑顺序是什么? newIt
我希望能够从本地存储中删除值而不是键,有什么线索吗? $(".delete").on("click", function() { localStorage.removeItem('Proper
我想删除数组中的一项,但它没有被删除。我有元素的 ID,但我不能使用它。你能告诉我如何使用 tasks 数组中的项目 ID 吗? function appendTaskToList(val, task
我正在尝试将帐户删除添加到我的应用程序中。我基于 API 使用具有多个值的一个 key 来存储帐户。 这是我添加帐户的方式: addAccount(state,account){ state.
我使用 nID 和 RemoveItem 过滤 CMFCPopupMenu 项。菜单在资源中定义。当 OnContextMenu 发生时,我会这样做 CMenu* pSubMenu = menu.Ge
QGraphicsScene::removeItem 有一些奇怪的地方,当我从场景中删除两个项目时,我收到以下消息: QGraphicsScene::removeItem: item 0x5edb28
我有这个代码用于保存来自 2 个输入的数据。当页面刷新两次(数据已保存)时,一切都很好,但在第三次刷新时,日期消失了。我知道这是因为 localStorage.removeItem ,但我需要在标签消
我正在使用 pnotify 插件向用户显示通知。但是,如果用户通过单击 X 图标关闭 1 个选项卡中的通知,我想删除所有选项卡上的通知。 我为此使用本地存储,每次向用户显示新通知时,它都会添加到本地存
使用 jQuery 和 localStorage,我想跟踪玩家赢得游戏的次数。每次提交表格时,获胜计数或失败计数都会增加一,具体取决于获胜者。计数器工作正常,但由于某种原因,清除计数的功能不起作用。我
努力解决这个问题...... 我正在尝试从我的 localStorage 对象中删除键/值对。但是,没有任何内容被删除。 (我也没有任何错误)。 我知道我可以通过键名删除有问题的键/值。这是该对象的示
如果我使用 localStorage.removeItem("key"); 删除一个项目,有什么方法可以检查删除是否成功?回调或 promise 之类的东西? 现在我是这样做的: if(localSt
为什么“localStorage.removeItem”会停止循环?如果我删除“localStorage.removeItem”并只保留“警报”,它会循环遍历整个过程,但对于“localStorage
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Deleting from localStorage: should I use delete or .re
这个问题在这里已经有了答案: Objc save a file to the tmp/ with URL it is not working (1 个回答) UIImage(contentsOfFi
我正在使用自定义项类的实例填充 QGraphicsScene(继承 QGraphicsPathItem)。在运行时的某个时候,我尝试通过调用从场景中删除一个项目(及其子项): delete pItem
我应该使用 clear() 清除 localStorage 中的所有内容,还是应该手动 removeItem() 我设置的那些在那个特定的网站上(很容易跟踪)? 我问是因为我不想最终清除用户的 loc
虽然我通过 PyQt 从 Python 使用 Qt,这个问题同样适用于纯 Qt,只是语法有点不同,问题是一样的: 当我们想要在场景中处理 QGraphicsItem 对象时,我们调用 scene.re
我创建了一个扩展 AbstractListModel 的 ComboBoxModel 类。我可以将项目添加到组合框,但是当我尝试删除时,出现异常 Exception in thread "AWT-Ev
我想知道如何实现这一目标。 我在 Activity A 中有一个 listView 。当检测到长按时,它将转到 checkMaxID(ID,iD); 。如果iD为最大,则会显示成功,删除列表,最后刷新
请告诉我,为什么 BindingList RemoveItem 方法受到保护?它的目的是什么?我知道我可以继承它,但问题是为什么要保护它 最佳答案 这是 protected ,因为 BindingLi
我是一名优秀的程序员,十分优秀!