本文介绍了HTML LocalStorage中的数据在其他窗口/选项卡中可用的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用HTML LocalStorage的网页。通常会同时打开此页面的多个选项卡/窗口。由于这些都使用相同的LocalStorage和LocalStorage不提供事务或类似,我想实现某种形式的互斥,以防止不同的选项卡/窗口以不受控制的方式覆盖彼此的数据。

I have a webpage that uses HTML LocalStorage. It is common to have multiple tabs/windows of this page open at the same time. Since these all use the same LocalStorage and LocalStorage does not provide transactions or similar, I would like to implement some form of mutual exclusion to prevent the different tabs/windows to overwrite each other's data in an uncontrolled manner.

我试图只是移植。

I tried to just port my test of the Burns/Lynch mutual exclusion algorithm to the browser by simply storing the boolean[] F in LocalStorage.

事情在FireFox中完美运行,但Chrome允许平均约1.3个进程(大多数只有一个,有时两个,甚至很少甚至3个或更多)同时进入关键部分,和Internet Explorer允许平均2个进程(大多数是1,2或3,有时甚至更多)。

Things work perfectly in FireFox, but Chrome allows an average of about 1.3 processes (mostly just one, sometimes two and very rarely even 3 or more) into the critical section at the same time, and Internet explorer allows an average of 2 processes (mostly 1, 2, or 3, sometimes even more) in.

由于,我的实现是非常重要的,我已经测试了它,否则,我能想出为什么会发生这种情况的唯一原因是在Chrome和IE在一个选项卡/窗口中写入LocalStorage与在所有其他选项卡/窗口中显示新值之间存在延迟。

Since the algorithm has been proven correct and my implementation is super-trivial and I've tested the heck out of it otherwise, the only reason that I can come up with for why this is happening is that in Chrome and IE there is a delay between when I write to LocalStorage in one tab/window and when the new value will be visible in all other tabs/windows.

这可能吗?如果是的话,是否有任何文件或对这些延误有任何保证?或者,更好的是,是否存在某种提交 - 或flush() - 我可以用来强制更改立即传播的调用?

Is this possible? If so, is there any documentation or are there any guarantees on these delays? Or, better yet, is there some kind of "commit"- or "flush()"-call that I can use to force the changes to propagate immediately?

更新:

我把一点测试往返时间:

I put together a little jsfiddle to test out the round-trip times:

// Get ID
var myID;
id = window.localStorage.getItem("id");
if (id==1) { myID = 1;    window.localStorage.setItem("id", 0); }
      else { myID = 0;    window.localStorage.setItem("id", 1); }

// Initialize statistics variables
var lastrun    = (new Date()).getTime();
var totaldelay = 0;
var count      = 0;
var checks     = 0;

document.documentElement.innerHTML = "ID: "+myID;

// Method that checks the round-trip time
function check() {
    window.setTimeout(check, 1); // Keep running
    value = window.localStorage.getItem("state");
    checks++;
    if (value==myID) return;
    window.localStorage.setItem("state", myID);
    now = new Date().getTime();
    totaldelay += now - lastrun;
    count++;
    lastrun = now;
    document.documentElement.innerHTML = "ID: "+myID+"<br/>"+
                                         "Number of checks: "+checks+"<br/>"+
                                         "Number of round-trips: "+count+"<br/>"+
                                         "Checks per round-trip: "+checks/count+"<br/>"+
                                         "Average round-trip time:"+totaldelay/count;
}

// Go!
window.setTimeout(check, 1000);

如果我在两个不同的窗口中运行这个小提琴,我在第二个窗口中打开以下数字:

If I run this fiddle in two different windows, I get the following numbers in the second window I opened:

 Browser          | Checks per round-trip | Average round-trip time
------------------+-----------------------+-------------------------
 Firefox 24.3.0   |                  1.00 |                  6.1 ms
 Chrome 46.0.2490 |                  1.06 |                  5.5 ms
 IE 10            |                 17.10 |                 60.2 ms


推荐答案

原来,Internet Explorer再次赢得竞争有史以来最糟糕的软件......

Turns out Internet Explorer again wins the competition for worst piece of software ever written...

IE11中实际上存在一个严重的错误(我也看到了IE10的问题)基本上无法使用LocalStorage如果用户打开多个选项卡,则可靠地可靠:每个选项卡实际上都获得了自己的同一存储位置的缓存版本,并且该缓存与实际存储之间的同步最多也是片状(如果它发生的话)。一个人根本不能依赖于将数据制作到其他标签/窗口,基本上就是这样。

There's actually a severe bug in IE11 (and I'm seeing issues with IE10 also) that basically makes it impossible to use LocalStorage reliably if the user opens more than one tab: Each tab essentially gets its own cached version of the same storage location and the synchronization between that cache and the actual storage is flaky at best (if it happens at all). One cannot at all rely on data making it to the other tab/window, basically ever.

这是官方的错误报告:

Here's the official bug-report: IE 11 - Local Storage - Synchronization issues

(当然,这只是)

我喜欢这个问题是如何在2013年开放的,甚至承认,但仍然没有得到修复...

I love how this issue was opened in 2013 and even acknowledged, but still hasn't been fixed...

不幸的是,在我的测试中,所发布的三个解决办法都没有让事情变得更好。

Unfortunately none of the three work-arounds posted make things any better in my tests.

换句话说:在IE10 / 11中,使用LocalStorage的交叉表/窗口通信基本上是不可能的。

In other words: Cross-tab/window communication using LocalStorage is basically impossible in IE10/11.

令人惊讶的是,cookie的同步windows / tabs似乎更加稳定ble(至少在我的IE10测试中)。所以也许这可以用作解决方法。

Surprisingly, the synchronization of cookies across windows/tabs seems to be a lot more stable (at least in my tests with IE10). So maybe that can be used as a work-around.

这篇关于HTML LocalStorage中的数据在其他窗口/选项卡中可用的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:02