在 localStorage 中存储变量太慢 [英] storing a variable in localStorage is too slow

查看:28
本文介绍了在 localStorage 中存储变量太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 jQuery 移动页面(#list 和 #show).#list 页面上有几个具有不同 ID 的项目.如果我点击第 5 项,ID 号 5 将存储在 localStorage 中,我将被重定向到页面 #show

I have two jQuery mobile pages (#list and #show). There are several items on the #list page with different IDs. If I click on item no.5, the ID no5 will be stored in localStorage and I will be redirected to page #show

现在的问题:将 ID 存储在 localStorage 中有效,但下一页显示的不是第 5 项,而是显示了一个旧项,之前在 localStorage 中.

Now the problem: Storing the ID in localStorage works, but the next page shows me not the item no.5, but it shows me an old item, that was in the localStorage before.

页面#list 中的脚本

localStorage.setItem("garageID", $(this).attr('id'));                           
window.location.replace("#show");

推荐答案

我也遇到了这个问题(不是在手机上:在 Chromium/linux 上).

I encountered this problem too (and not on a mobile : on Chromium/linux).

由于似乎没有基于回调的 API,我使用超时修复"它,以防止"页面在 setItem 操作完成之前关闭:

As there doesn't seem to be a callback based API, I "fixed" it with a timeout which "prevents" the page to be closed before the setItem action is done :

localStorage.setItem(name, value);                           
setTimeout(function(){
     // change location
}, 50);

0 的超时可能就足够了,但由于我没有找到任何规范(这可能是在错误领域)并且问题并未始终如一地重现,因此我没有冒险.如果你愿意,你可以循环测试:

A timeout of 0 might be enough but as I didn't find any specification (it's probably in the realm of bugs) and the problem isn't consistently reproduced I didn't take any chance. If you want you might test in a loop :

function setLocalStorageAndLeave(name, value, newLocation){
    value = value.toString(); // to prevent infinite loops
    localStorage.setItem(name, value);
    (function one(){
         if (localStorage.getItem(name) === value) {
            window.location = newLocation;
         } else {
            setTimeout(one, 30);
         }
    })();
}

但我不知道 localStorage.getItem 返回正确值的事实如何保证它真的以永久方式编写,因为没有可中断行为的规范,我不知道如果规范的以下部分可以合法地解释为允许浏览器在离开页面时忘记在磁盘上转储:

But I don't see how the fact that localStorage.getItem returns the right value would guarantee it's really written in a permanent way as there's no specification of the interruptable behavior, I don't know if the following part of the spec can be legitimately interpreted as meaning the browser is allowed to forget about dumping on disk when it leaves the page :

本规范不要求上述方法等到数据已物理写入磁盘.只有在什么方面保持一致不同的脚本访问相同的底层键/值列表双见是必需的.

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.

在您的确切情况下,解决方案可能是简单地滚动到具有该给定名称的元素以避免更改页面.

In your precise case, a solution might be to simply scroll to the element with that given name to avoid changing page.

注意假定的错误:

我没有找到也没有填写任何错误报告,因为我发现很难重现.在我在 Chromium/linux 上观察到的情况下,它发生在 delete 操作中.

I didn't find nor fill any bug report as I find it hard to reproduce. In the cases I observed on Chromium/linux it happened with the delete operation.

这篇关于在 localStorage 中存储变量太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆