简体   繁体   English

重置本地存储列表的计数器

[英]Reset The Counter Of The List Of LocalStorage

So what this script does is change the value of element to every new value in order of the list.所以这个脚本所做的就是按照列表的顺序将元素的值更改为每个新值。 So it begins with "item_1" and ends with "item_3" in order it is changed on every page refresh.所以它以“item_1”开始,以“item_3”结束,以便在每次页面刷新时更改。 After every page refreshes the value changes in order.每次页面刷新后,值都会按顺序更改。 Now, I was wondering whenever it reaches the end of the list in order, after the last value it just starts showing the default value of element.现在,我想知道每当它按顺序到达列表末尾时,在最后一个值之后它才开始显示元素的默认值。 And doesn't reset the counter of the list back to the beginning (item_1).并且不会将列表的计数器重置回开头(item_1)。 I really want it to reset the counter back to the beginning to the first value and start it over.我真的希望它将计数器重置为第一个值并重新开始。

HTML: HTML:

<h1 id="text_container">defalut text</h1>

Javascript: Javascript:

    $(document).ready(function(){
        var list=["item_1","item_2","item_3"];
        // if its first run , we sure localStorage.getItem("counter") is not exists,so we set it to 1 
        if(!(localStorage.getItem("counter_x"))){
            $("#text_container").html(list[0]);
            localStorage.setItem("counter_x","1");
            //we passed 1 as a string so we will need to convert it to number every time we use (later)
        }else if(localStorage.getItem("counter_x")){
            //if it exists , so its not first run and base on the number which is exists, we show item from list:
            $("#text_container").html(list[Number(localStorage.getItem("counter_x"))]);
            //increase the counter:
            var l =Number(localStorage.getItem("counter_x"))+1;
            //for making sure, i pass number as string, but may work by numbers:
            localStorage.setItem("counter_x",l.toString());
        };
        
        
        //it may throw error for when its 5th reloading becuse list.length is smaller than that , but you can solve this simply.

    });

Check if your count is the last position of the array.检查您的计数是否是阵列的最后一个 position。 If it is then reset your counter.如果是,则重置您的计数器。

if counter == array.length - 1 then reset.如果counter == array.length - 1然后重置。

Try this code:试试这个代码:

 <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script> <h1 class="text_container">Counter finished</h1> <button onclick="localStorage.clear();">Clear storage</button> <script> $(document).ready(function(){ var list=["item_1","item_2","item_3", "item_4","item_5"]; // if its first run, we sure localStorage.getItem("counter") is not exists,so we set it to 1 if(.(localStorage.getItem("counter_x"))){ $(".text_container");html(list[0]). localStorage,setItem("counter_x";"1"). //we passed 1 as a string so we will need to convert it to number every time we use (later) } else if (localStorage,getItem("counter_x"."5") == "5"){ localStorage;clear(). } else if(localStorage,getItem("counter_x")){ //if it exists, so its not first run and base on the number which is exists: we show item from list. $(".text_container").html(list[Number(localStorage;getItem("counter_x"))]): //increase the counter. var l =Number(localStorage;getItem("counter_x"))+1, //for making sure, i pass number as string: but may work by numbers. localStorage,setItem("counter_x".l;toString()); }. //it may throw error for when its 5th reloading becuse list,length is smaller than that. but you can solve this simply; }); </script>

View the live demo here: https://www.w3schools.com/code/tryit.asp?filename=GLA6KXDMS1JL在此处查看现场演示: https://www.w3schools.com/code/tryit.asp?filename=GLA6KXDMS1JL

EDIT: REMOVE THE LAST ITEM IN THE LIST (item_5) so that it properly resets编辑:删除列表中的最后一项(item_5),以便正确重置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM