2018-12-10 每日随笔

(1)js获取当前时间戳

let time = new Date().getTime()

(2)关于charAt()

charAt() 方法可返回指定位置的字符
比方说:
在字符串 "Hello world!" 中,我们将返回位置 1 的字符:


以上代码的输出是:
e

(3)关于设置cookie和获取cookie

假如在vue的method里写

methods: {
  //设置cookie
      setCookie: function (cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        console.info(cname + "=" + cvalue + "; " + expires);
        document.cookie = cname + "=" + cvalue + "; " + expires;
        console.info(document.cookie);
      },
      //获取cookie
      getCookie: function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        console.log("获取cookie,现在循环")
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i];
          console.log(c)
          while (c.charAt(0) == ' ') c = c.substring(1);
          if (c.indexOf(name) != -1){
            return c.substring(name.length+1, c.length);
          }
        }
        return "";
      },
      //清除cookie
      clearCookie: function () {
        this.setCookie("username", "", -1);
      },
      checkCookie: function () {
        var user = this.getCookie("username");
        if (user != "") {
          alert("Welcome again " + user);
        } else {
          user = prompt("Please enter your name:", "");
          if (user != "" && user != null) {
            this.setCookie("username", user, 365);
          }
        }
      }
    }

(4)substring()

substring() 方法用于提取字符串中介于两个指定下标之间的字符。

你可能感兴趣的:(2018-12-10 每日随笔)