Javascript

刷新界面

// 作用:刷新页面
location.reload();          // 默认从缓存加载(可能不获取最新资源)

// 保持登录
location.reload(true);      // 强制从服务器重新加载(绕过缓存)
// Get Cookie
function getCookie(cookie_name) {
    // 构建正则表达式以匹配指定的 cookie 名称
    const nameEQ = cookie_name+ "=";
    const ca = document.cookie.split(';'); // 将所有 cookies 按分号拆分成数组
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        // 去除前后空格
        while (c.charAt(0) === ' ') {
            c = c.substring(1, c.length);
        }
        // 如果找到匹配的 cookie 名称,则返回其值
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    // 如果没有找到,则返回空字符串
    return "";
}

/**
 * 设置Cookie
 * @param {string} name 键名
 * @param {string} value 键值
 * @param {number} days 有效期天数
 * @param {string} path 作用路径
 */
function setCookie(name, value, days = 30, path = '/') {
  const date = new Date();
  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  const expires = `expires=${date.toUTCString()}`;
  document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; path=${path}`;
}

LocalStorage

// localStorage 是一种 Web 存储机制,允许在用户的浏览器中存储数据。与 sessionStorage 不同,localStorage 中的数据在浏览器关闭后仍然存在,直到被明确删除。这使得 localStorage 成为存储需要持久化的数据的理想选择。

// localStorage 只支持字符串类型存储,因此如果需要存储其他数据类型(如对象或数组),可以使用 JSON.stringify 和 JSON.parse 方法进行转换。

// 删除数据
localStorage.removeItem('key');

// 清空
localStorage.clear();

// 设置页面每隔一定时间自动刷新
function set_auto_reload() {
    // 获取数据
    if (localStorage.getItem("autoRefreshEnabled") === "true") {
        localStorage.setItem("autoRefreshEnabled", false);
        showAlert("停止刷新!")
    }else {
        // 设置数据
        localStorage.setItem("autoRefreshEnabled", true);
        showAlert("开始刷新!")
    }
}

箭头函数

// 箭头函数
() => copyText('cluster')
==>
// 省略传统函数的 function 关键字和大括号
function() {
    copyText('cluster');
}

// 箭头函数传参
err => {
    showAlert("复制失败!");
}
==>
function(err) {
  showAlert("复制失败!");
}

复制文本

// navigator.clipboard.writeText()

// 复制内容到剪切板(异步)
navigator.clipboard.writeText(text).then(() => {
	showAlert("已复制!");
}).catch(err => {
    showAlert("复制失败!");
});	

物理机 2025-07-17
物理机交付 2025-07-17

评论区