- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们在 localstorage
中存储了一些数据,我们正在使用 window.setInterval()
每分钟定期更新它。在这段时间里,我们不断地读写数据。
是否有可能因为我们正在使用 setInterval()
而发生并发问题,因为多个选项卡可以同时修改 localstorage
中的数据?
编辑 1:详细解释场景:
每个选项卡都根据一个键(比如 xyz
)和 setInterval()
将一些数据写入 localstorage
,存在于运行的 javascript 中, 不断检查 xyz
关键数据。如果某些数据存在于 key 中,则 setInterval
回调会将其发送到后端。运行相同脚本的每个选项卡将读取 xyz
键并在执行一些逻辑后将一些数据附加到现有值。
我怀疑可能会出现并发问题,比如一个选项卡可能正在读取 xyz
键并将数据添加到本地存储,而另一个选项卡可能同时在做同样的事情。现在两者都会尝试同时发送数据,因此我可能会在后端收到相同的数据 2 次。
最佳答案
Is it possible that concurrency issues can occur because we're using SetInterval() as multiple tabs can modify the data in local storage simultaneously?
这有两个方面:
getItem
/setItem
(及其等效访问器)是原子的吗?getItem
/setItem
调用的不同选项卡/窗口是否可以交错调用这些调用?在#1 上,令人惊讶的是,storage specification似乎没有正面解决这个问题。在“注释”中它说:
Note
This specification does not require that the above methods wait until the data has been physically written to disk. Only consistency in what different scripts accessing the same underlying list of key/value pairs see is required.
...这对我来说意味着 getItem
/setItem
将是原子的——也就是说,如果两个线程在字面上同时调用getItem
/setItem
。
关于 #2,我认为没有任何保证,不。如果每个选项卡/窗口都有自己的线程,那么理论上这些线程中的两个可以同时进入执行这些更新的代码块。有时,选项卡/窗口共享一个线程,在这种情况下你会很安全,但是......
我会避免在 localStorage
中有很多需要以协调方式更新的不同条目。相反,我会使用带有结构的单个条目。我通常为此使用 JSON。所以获取数据看起来像这样:
let data = JSON.parse(localStorage.getItem("the-data")) || {/*...default structure here...*/};
保存起来是这样的:
localStorage.setItem("the-data", JSON.stringify(data));
所以你可以做
let data = JSON.parse(localStorage.getItem("the-data")) || {/*...default structure here...*/};
// ...modify the various parts of `data`...
localStorage.setItem("the-data", JSON.stringify(data));
然后您将在线程之间进行简单的竞争(最后写入的线程获胜),但存储的数据将是一致的。
关于javascript - 带有多个选项卡的 localstorage 和 setInterval(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805408/
这个问题已经有答案了: JavaScript closure inside loops – simple practical example (45 个回答) 已关闭 8 年前。 我试图创建多个 se
这是我的情况,我需要加快函数运行时间,所以 setInterval 不是一个明智的选择,对吧?因为每次至少要花费 4 毫秒。 所以,我可以将 setInterval 函数更改为 requestAnim
我正在尝试下面的代码,看看是否可以将 setInterval Id 存储为关联数组中唯一键的值,然后通过使用唯一值作为键来停止被调用函数中的间隔,如下所示我将 setInterval Id 作为它的值
我花了很长时间试图弄清楚如何使用具有 CSS 样式缓动功能的 Google Maps API 为折线上的符号设置动画。我让它工作 with this Gist和 this Google Maps AP
我是这里的 JS 新手,正在研究一个在给定时间段后开始的倒数计时器。基本上,当用户单击按钮时时钟开始,第二个时钟在第一个计时器用完时开始。我知道“setInterval”是完成这个的最好方法。我面临的
嗨, friend 们,我想只使用一个 setInterval 函数来运行我的代码。目前我正在使用两个 setInterval's 来实现我的目标,我们可以只使用一个 'setInterval' 来获
我的“setInterval”函数有问题,我想在将鼠标悬停在 div 上时启动该函数,但是 setInterval 在我将鼠标悬停在 div 上时第一次起作用=>如果我停留在div,它不会继续改变图片
我想展示两次:一次在你的国家,一次在日本用JS 问题是第二个 setInterval 停止了第一个,我不知道如何进行两次运行。 完整代码 In your region:
好吧,这个问题有几个问题。 首先,我要求 setTimeout() 和 setInterval() 我见过几种不同的调用方式,我想知道哪种方式最适合这种情况。 我正在制作一个 js/canvas 游戏
setInterval(function () { //======= //code //======== if(--timer&etype="; } },1000); 这里定时器结束后,而不是重定
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我的前老板有一个 weird bug where when he used setInterval with a long delay interval : setInterval(func, 300
这个问题已经有答案了: How does the "this" keyword work, and when should it be used? (22 个回答) How to access the
我的印象是 setInterval("/*some code*/", time) 相当于 setInterval(function() { /*some code*/ }, time) 显然不是
我对 JavaScript 和 NodeJS 非常陌生,我只是想了解 NodeJS 中的发射器模式。当我尝试使用 setInterval 函数每秒发出一个刻度事件时,程序似乎工作正常:-
我有一个简单的 setTimeout 函数,它在特定时间运行并且工作正常: var now = new Date(); var milliTillExec = new Date(now.getFull
在本教程中,您将借助示例了解 JavaScript setInterval() 方法。 在 JavaScript 中,可以在指定的时间间隔内执行一段代码。这些时间间隔称为定时事件。 有
Js: function cometConnect(){ $.ajax({ cache:false, type:"post", d
LE2。关于如何解决此问题的任何其他想法? 我有这段代码,但不知道为什么不能正常工作: $(function autorun() { if ($("#contactForm").is(":visibl
这个问题在这里已经有了答案: How to make a setInterval stop after some time or after a number of actions? (6 个答案)
我是一名优秀的程序员,十分优秀!