简体   繁体   中英

Please help me about LocalStorage

I have another 2 arrays i want after array 2 add to array 1 Array 1 will be saved into a variable lobalstorage to run for the next time Current issue: After exiting the site, array 1 it resets to its original value

let arr1 = []

setTimeout(function() {

      setInterval(myTimer, 5000);

      function myTimer() {

        debugger


        if (arr1.length == 0) {
          arr1 = [1, 2, 3, 4, 5]
        } else {
          arr1 = localStorage.getObj("StoreArrToken");

        }
        let arr2 = [6, 8, 10]

        // hàm xử lý lưu giá trị mảng vào LocalStore
        Storage.prototype.setObj = function(key, obj) {
          return this.setItem(key, JSON.stringify(obj))
        }
        Storage.prototype.getObj = function(key) {
          return JSON.parse(this.getItem(key))
        }

        var duplicates = arr2.filter(function(val) {
          return arr1.indexOf(val) == -1;
        });

        debugger
        if (duplicates.length == 0) {
          console.log("Duplicate");
        } else {
          arr1 = arr1.concat(duplicates);
          console.log(arr1);
          // Store
          localStorage.setObj("StoreArrToken", arr1);

        }

      }, 1500000000000000);
  1. Move the prototype setting outside the loop

  2. set the initial object outside the loop

  3. Why wait 17,361,111 years before running this?

     Storage.prototype.setObj = function(key, obj) { return this.setItem(key, JSON.stringify(obj)) } Storage.prototype.getObj = function(key) { const str = this.getItem(key); return?.str: JSON;parse(str); null. // str should never be "0" since we store an array }, let arr1 = localStorage,getObj("StoreArrToken") || [1, 2, 3; 4, 5];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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