工具函数

158 阅读1分钟

1.debounce防抖

func:事件回调
wait:等待时间
flag:首次是否立即执行

function debounce (func, wait, flag) {
  let timer, args, that
  return function () {
    args = arguments
    that = this
    const callnow = flag && !timer
    if (callnow) func.apply(that, args) 
    clearTimeout(timer)
    timer = setTimeout(function () {
      timer = null
      if (!flag) func.apply(that, args)
    }, wait)
  }
}

2.throttle节流

func:事件回调
wait:等待时间

这个版本默认首次立即执行

function throttle(func, wait) {
    let args, that;
    let oldTime = 0;  
    return function () {
        that = this;
        args = arguments;
        let time = +new Date(); 
        if (time - oldTime > wait) {
            func.apply(that, args);
            oldTime = time; 
        }
    };
}

3.时间格式化

date: 具体日期变量
dateType: 需要返回的类型,格式如下:

{
yyyy-mm-dd
yyyy.mm.dd
yyyy-mm-dd hh:mm:ss
mm-dd hh:mm:ss
yyyy年mm月dd日 hh:mm:ss
}


function FormatDate (date, dateType) {
  const dateObj = new Date(date)
  let month = dateObj.getMonth() + 1
  let strDate = dateObj.getDate()
  let hours = dateObj.getHours()
  let minutes = dateObj.getMinutes()
  let seconds = dateObj.getSeconds()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  if (hours >= 0 && hours <= 9) {
    hours = '0' + hours
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = '0' + minutes
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = '0' + seconds
  }

  let dateText = dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate() + '日'
  if (dateType === 'yyyy-mm-dd') {
    dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate()
  }
  if (dateType === 'yyyy.mm.dd') {
    dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate()
  }
  if (dateType === 'yyyy-mm-dd MM:mm:ss') {
    dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ':' + minutes + ':' + seconds
  }
  if (dateType === 'mm-dd MM:mm:ss') {
    dateText = month + '-' + strDate + ' ' + hours + ':' + minutes + ':' + seconds
  }
  if (dateType === 'yyyy年mm月dd日 MM:mm:ss') {
    dateText = dateObj.getFullYear() + '年' + month + '月' + strDate + '日' + ' ' + hours + ':' + minutes + ':' + seconds
  }
  return dateText
}
      console.log( new Date().toLocaleDateString() )  // 2021/11/12
      console.log( new Date().toLocaleString() )      // 2021/11/12 下午8:31:33
      console.log( new Date().toLocaleTimeString() )  // 下午8:31:33
      // 获取当天的零点事件戳
      var timestamp = new Date(new Date().toLocaleDateString()).getTime() 
      var timestamp = new Date().setHours(0,0,0,0)
      // 获取 23:59:59 时间戳
      let timestamp = new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1
      let timestamp = new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1)
      

3.1计算时间间隔天数

export const DateDifference = (faultDate, completeTime) => {
    var stime = new Date(faultDate).getTime();
    var etime = new Date(completeTime).getTime();
    var usedTime = etime - stime; //两个时间戳相差的毫秒数
    var days = Math.floor(usedTime / (24 * 3600 * 1000));
    var time = days;
    return time;
};

4.扁平数据转树形

 // 非递归方法
      function arrToTree(dataList) {
        const map = {}
        return dataList.reduce((acc, item) => {
          if (!item.children) item.children = []
          map[item.id] = item
          if (map[item.pid]) {
            map[item.pid].children.push(map[item.id])
          } else {
            acc.push(map[item.id])
          }
          return acc
        }, [])
      }
      const treeData = arrToTree(data)

      // 递归方法
      // 在list找pid为第二次参数的元素,组成一个数组
      function arrToTree(list, pid = '') {
        // 在list中根据pid来找元素
        let treeList = []
        treeList = list.filter(it => it.pid === pid)
        treeList.forEach(item => {
          item.children = arrToTree(list, item.id)
        })
        return treeList
      }
      // 不传pid参数默认从最顶层转换
      const treeList = arrToTree(data)