将值存储到 localStorage 的顺序是同步的吗?

Is order of storing values to localStorage synchronous?

对不起我糟糕的英语。

我正在使用 localStorage 来存储一些属性。每个属性都有自己的键和值,键和值是动态生成的。所以我不知道会有多少人。

第 1 页的所有属性都已保存(单击一次)。

在第 2 页上,我调用了 "storage" 事件。我需要读取所有键和值并使用它们重新呈现页面内容。

问题是每个 key/value 对都会触发 "storage" 事件。而且我不想为每个 key/value 对重新呈现 page2 的内容。我想等到所有属性都保存在 page1 上,然后重新呈现 page2 的内容。

所以我需要确保所有属性都保存在第 1 页上,然后我需要在第 2 页上阅读它们。

我的问题:localStorage的存储过程是同步的吗? "Synchronous" 对我来说意味着同时只执行一个 setItem 命令,并且所有 setItem 命令都按照我编写的确切顺序执行。 所以我什么时候会写:

(loop start)    
localStorage.setItem("bar1", foo1)
localStorage.setItem("bar2", foo2)
...
localStorage.setItem("bar100", foo100)
(loop end)    

我可以确定所有项目都在 ("bar100", foo100) 之前保存了吗?

或者可能有任何问题,并且 ("bar100", foo100) 可能在之前的任何项目之前被保存?

亚历克斯

来自 W3C 规范:

Whenever the properties of a localStorage attribute's Storage object are to be examined, returned, set, or deleted, whether as part of a direct property access, when checking for the presence of a property, during property enumeration, when determining the number of properties present, or as part of the execution of any of the methods or attributes defined on the Storage interface, the user agent must first obtain the storage mutex.

这意味着它是运行同步方式,但是,这不能保证页面重新加载后存储项目的顺序。

但是要回答你的问题,是的,"bar100" 将最后写入本地存储。