JS实现localStorage自动保存功能

通过localStorage保存当前浏览器中的数据。

在日志中经常使用这个功能,直接上代码。

function test() {
  if (!window.localStorage) {
    alert("Your browser don't adequate localStorage");
  }
  else {
    var spanObj = document.getElementById('s1');
    var saveTimer = setInterval(function() {
      var str = "";
      if (document.all) {
        str = document.frames[1].document.body.innerHTML;
      }
      else {
        str = document.getElementById("ueditor_0").contentDocument.body.innerHTML;
      }
      if (str.length > 20 && (str.indexOf("。") > -1 || str.indexOf(",") > -1)) {
        localStorage.setItem("ctValue", str);
        var d = new Date();
        var YMDHMS = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" +
          d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        spanObj.innerText = 'time ' + YMDHMS;
        setTimeout(function() { spanObj.innerText = ''; }, 5000);
      }
    }, 25000);

    function stoplocs() {
      clearInterval(saveTimer);
      //localStorage.removeItem("ctValue");
    }

    function showlocs() {
      var html = localStorage.getItem("ctValue");
      editor.setContent(html);
      //alert(localStorage.getItem("ctValue"));
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_41697143/article/details/82758834
今日推荐