Skip to content

常用代码

节流

js
/**
 * 节流,多次触发,间隔时间段执行
 * @param {Function} func
 * @param {Int} wait
 * @param {Object} options
 */
export function throttle(func, wait = 500, options) {
  //container.onmousemove = throttle(getUserAction, 1000);
  var timeout, context, args;
  var previous = 0;
  if (!options) options = { leading: false, trailing: true };

  var later = function () {
    previous = options.leading === false ? 0 : new Date().getTime();
    timeout = null;
    func.apply(context, args);
    if (!timeout) context = args = null;
  };

  var throttled = function () {
    var now = new Date().getTime();
    if (!previous && options.leading === false) previous = now;
    var remaining = wait - (now - previous);
    context = this;
    args = arguments;
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout);
        timeout = null;
      }
      previous = now;
      func.apply(context, args);
      if (!timeout) context = args = null;
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining);
    }
  };
  return throttled;
}
/**
 * 节流,多次触发,间隔时间段执行
 * @param {Function} func
 * @param {Int} wait
 * @param {Object} options
 */
export function throttle(func, wait = 500, options) {
  //container.onmousemove = throttle(getUserAction, 1000);
  var timeout, context, args;
  var previous = 0;
  if (!options) options = { leading: false, trailing: true };

  var later = function () {
    previous = options.leading === false ? 0 : new Date().getTime();
    timeout = null;
    func.apply(context, args);
    if (!timeout) context = args = null;
  };

  var throttled = function () {
    var now = new Date().getTime();
    if (!previous && options.leading === false) previous = now;
    var remaining = wait - (now - previous);
    context = this;
    args = arguments;
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout);
        timeout = null;
      }
      previous = now;
      func.apply(context, args);
      if (!timeout) context = args = null;
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining);
    }
  };
  return throttled;
}

防抖

js
/**
 *
 * @param {*} func 要进行debouce的函数
 * @param {*} wait 等待时间,默认500ms
 * @param {*} immediate 是否立即执行
 */
export function debounce(func, wait = 500, immediate = false) {
  var timeout;
  return function () {
    var context = this;
    var args = arguments;

    if (timeout) clearTimeout(timeout);
    if (immediate) {
      // 如果已经执行过,不再执行
      var callNow = !timeout;
      timeout = setTimeout(function () {
        timeout = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args);
      }, wait);
    }
  };
}
/**
 *
 * @param {*} func 要进行debouce的函数
 * @param {*} wait 等待时间,默认500ms
 * @param {*} immediate 是否立即执行
 */
export function debounce(func, wait = 500, immediate = false) {
  var timeout;
  return function () {
    var context = this;
    var args = arguments;

    if (timeout) clearTimeout(timeout);
    if (immediate) {
      // 如果已经执行过,不再执行
      var callNow = !timeout;
      timeout = setTimeout(function () {
        timeout = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args);
      }, wait);
    }
  };
}

环境

js
const UA = window.navigator.userAgent.toLowerCase();

// Android
const isAndroid = /android/.test(UA);

// IOS
const isIOS = /iphone|ipad|ipod|ios/.test(UA);

// 浏览器环境
const inBrowser = typeof window !== "undefined";

// IE
const isIE = /msie|trident/.test(UA);

// Edge
const isEdge = UA.indexOf("edge/") > 0;

// Chrome
const isChrome = /chrome\/\d+/.test(UA) && !isEdge;

// 微信
const isWeChat = /micromessenger/.test(UA);

// 移动端
const isMobile = "ontouchstart" in window;
const UA = window.navigator.userAgent.toLowerCase();

// Android
const isAndroid = /android/.test(UA);

// IOS
const isIOS = /iphone|ipad|ipod|ios/.test(UA);

// 浏览器环境
const inBrowser = typeof window !== "undefined";

// IE
const isIE = /msie|trident/.test(UA);

// Edge
const isEdge = UA.indexOf("edge/") > 0;

// Chrome
const isChrome = /chrome\/\d+/.test(UA) && !isEdge;

// 微信
const isWeChat = /micromessenger/.test(UA);

// 移动端
const isMobile = "ontouchstart" in window;

判断类型

js
const toString = (value: any) => Object.prototype.toString.call(value);
function getTypeName(value: any) {
  if (value === null) return "null";

  const type = typeof value;
  if (type === "object" || type === "function") {
    return toString(value).slice(8, -1).toLowerCase();
  } else {
    return type;
  }
}
const toString = (value: any) => Object.prototype.toString.call(value);
function getTypeName(value: any) {
  if (value === null) return "null";

  const type = typeof value;
  if (type === "object" || type === "function") {
    return toString(value).slice(8, -1).toLowerCase();
  } else {
    return type;
  }
}

提取身份信息

js
function getIdCardInfo(idCard, separator = "/") {
  if (
    !/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(
      idCard
    )
  ) {
    throw Error(`${idCard}不是一个身份证号码`);
  }
  // 提取 idCard 中的字符
  const idSubstr = (s, e) => idCard.substr(s, e);
  // 拼接日期
  const splice = (d) => d.join(separator);
  // 获取出生年月日 性别(0 女 1 男)
  let birthday, gender;
  if (idCard.length === 18) {
    birthday = splice([idSubstr(6, 4), idSubstr(10, 2), idSubstr(12, 2)]);
    gender = idSubstr(-2, 1) & 1;
  } else {
    birthday = splice(idSubstr(6, 2), idSubstr(8, 2), idSubstr(10, 2));
    gender = idSubstr(-1, 1) & 1;
  }
  // 获取年龄(实岁)
  const birthDate = new Date(birthday);
  const newDate = new Date();
  const year = newDate.getFullYear();
  let age = year - birthDate.getFullYear();
  if (newDate < new Date(splice([year, birthday.substring(5)]))) {
    age--;
  }
  return {
    age,
    birthday,
    gender,
  };
}
function getIdCardInfo(idCard, separator = "/") {
  if (
    !/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(
      idCard
    )
  ) {
    throw Error(`${idCard}不是一个身份证号码`);
  }
  // 提取 idCard 中的字符
  const idSubstr = (s, e) => idCard.substr(s, e);
  // 拼接日期
  const splice = (d) => d.join(separator);
  // 获取出生年月日 性别(0 女 1 男)
  let birthday, gender;
  if (idCard.length === 18) {
    birthday = splice([idSubstr(6, 4), idSubstr(10, 2), idSubstr(12, 2)]);
    gender = idSubstr(-2, 1) & 1;
  } else {
    birthday = splice(idSubstr(6, 2), idSubstr(8, 2), idSubstr(10, 2));
    gender = idSubstr(-1, 1) & 1;
  }
  // 获取年龄(实岁)
  const birthDate = new Date(birthday);
  const newDate = new Date();
  const year = newDate.getFullYear();
  let age = year - birthDate.getFullYear();
  if (newDate < new Date(splice([year, birthday.substring(5)]))) {
    age--;
  }
  return {
    age,
    birthday,
    gender,
  };
}