计算localStorage空间的用法 [英] Calculating usage of localStorage space

查看:211
本文介绍了计算localStorage空间的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bespin编辑器和HTML5的localStorage创建一个应用程序。它在本地存储所有文件并有助于语法,使用JSLint和其他一些CSS和HTML解析器来帮助用户。



我想计算localStorage限制的数量已被使用,实际有多少。 今天可能吗?我想不要简单地计算存储的位数。但话又说回来我不知道还有什么我无法衡量自己。

解决方案

我没找到在我需要的浏览器上获得剩余限制的通用方法,但我确实发现当你达到限制时会弹出一条错误消息。这在每个浏览器中都是不同的。



为了最大限度地使用这个小脚本:

  for(var i = 0,data =m; i< 40; i ++){
try {
localStorage.setItem(DATA,data) ;
data = data + data;
} catch(e){
var storageSize = Math.round(JSON.stringify(localStorage).length / 1024);
console.log(LIMIT REACHED:(+ i +)+ storageSize +K);
console.log(e);
休息;
}
}
localStorage.removeItem(DATA);

从那里我得到了这个信息:



谷歌浏览器




  • DOMException:


    • code:22

    • 消息:无法在'存储'上执行'setItem':设置'数据'的值超出了配额。

    • name:QuotaExceededError




Mozilla Firefox




  • DOMException:


    • 代码:1014

    • 消息:达到持久存储最大大小

    • 名称:NS_ERROR_DOM_QUOTA_REACHED




Safari




  • DOMException:


    • 代码:22

    • 消息:QuotaExceededError:DOM Exception 22

    • name:QuotaExceededError




Internet Explorer,Edge (社区)




  • DOMException:


    • 代码:22

    • 消息:QuotaExceededError

    • name:QuotaExceededError







  • 我的解决方案



    到目前为止,我的解决方案是每次用户保存时添加额外的电话任何东西。如果异常被捕获,那么我会告诉他们他们的存储容量已经不足。






    编辑:删除添加的数据



    我忘了提到为了实际工作你需要删除 DATA 最初设置的项目。上面使用 removeItem()函数反映了这一变化。 / p>

    I am creating an app using the Bespin editor and HTML5's localStorage. It stores all files locally and helps with grammar, uses JSLint and some other parsers for CSS and HTML to aid the user.

    I want to calculate how much of the localStorage limit has been used and how much there actually is. Is this possible today? I was thinking for not to simply calculate the bits that are stored. But then again I'm not sure what more is there that I can't measure myself.

    解决方案

    I didn't find a universal way to get the remaining limit on the browsers I needed, but I did find out that when you do reach the limit there is an error message that pops up. This is of-course different in each browser.

    To max it out I used this little script:

    for (var i = 0, data = "m"; i < 40; i++) {
        try { 
            localStorage.setItem("DATA", data);
            data = data + data;
        } catch(e) {
            var storageSize = Math.round(JSON.stringify(localStorage).length / 1024);
            console.log("LIMIT REACHED: (" + i + ") " + storageSize + "K");
            console.log(e);
            break;
        }
    }
    localStorage.removeItem("DATA");
    

    From that I got this information:

    Google Chrome

    • DOMException:
      • code: 22
      • message: "Failed to execute 'setItem' on 'Storage': Setting the value of 'data' exceeded the quota."
      • name: "QuotaExceededError"

    Mozilla Firefox

    • DOMException:
      • code: 1014
      • message: "Persistent storage maximum size reached"
      • name: "NS_ERROR_DOM_QUOTA_REACHED"

    Safari

    • DOMException:
      • code: 22
      • message: "QuotaExceededError: DOM Exception 22"
      • name: "QuotaExceededError"

    Internet Explorer, Edge (community)

    • DOMException:
      • code: 22
      • message: "QuotaExceededError"
      • name: "QuotaExceededError"

    My solution

    So far my solution is to add an extra call each time the user would save anything. And if the exception is caught then I would tell them that they are running out of storage capacity.


    Edit: Delete the added data

    I forgot to mention that for this to actually work you would need to delete the DATA item that was set originally. The change is reflected above by using the removeItem() function.

    这篇关于计算localStorage空间的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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