如何限制数据存储到本地存储并检索它? [英] How to limit storage of data onto local storage and retrieve it?

查看:173
本文介绍了如何限制数据存储到本地存储并检索它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我需要限制添加到本地存储的数据。如果用户添加第6项,则应该删除第一项,我只需要存储5个项目。我完成了那部分但是在检索它时,它检索删除的数据并跳过第四项。有什么想法?

Hi I need to limit the data being added onto my local storage. I only need 5 items to be stored if the user adds the 6th item the first item should be removed. I accomplished that portion but when retrieving it, it retrieves the removed data and skips on the fourth item. Any ideas?

我如何设置东西

 var _i = 0 //global;
 storage.setItem(_i, URL);

checkLimit

checkLimit

if(storage.length >= 5)
{
   var _a = storage.getItem(0);
   storage.removeItem( _a );
   _a += 1;
}

检索时

for( var _x = 1; _x <= storage.length - 1; _x++)
{
  var _url = storage.getItem(_x);
}



更新



哦,我忘了提到当我使用键索引获取项目时它不会显示删除的数据,但不知何故会跳过第四个。

update

oh i forgot to mention that when i use key index to get item it doesnt display the removed data but somehow skips the fourth one.

现在它没有跳过第四条记录,但它显示了删除的数据,但没有显示最后添加的数据。例如

Now it doesn't skip the fourth record but it shows the removed data and doesn't show the last added data. e.g

the //first
avengers //second
are //third
coming //fourth
now //fifth
excited //sixth

因为我具体限制为五(5) 应该从列表中删除,并且应该添加 excited 。它做得很好但是当我关闭应用程序并尝试检索它返回的数据时

Since i specifically limit it to five (5) the should be removed from the list and excited should be added. its doing fine but when i close the application and try to retrieve the data it returns

the
avengers
are
coming
now

并且它不显示兴奋我有意义吗?

and it doesn't shows excited Am i making sense?

推荐答案

您不能依赖Web存储密钥的顺序。请检查此问题: HTML5 localStorage密钥订单

如果您要执行此操作这个,只需使用一个存储在localStorage中的数组对象。

You can't rely on the order of Web Storage keys. Check this question: HTML5 localStorage key order
If you want to do this, just use an array object which will be stored in localStorage.

这篇关于如何限制数据存储到本地存储并检索它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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