工作常用函数封装

2018-04-23  本文已影响0人  webj

判断是否为数字

export function isNumber(str) {

  return /^[0-9.]*$/.test(str);

}

debounce与throttle用法

export function debounce(fn, wait = 250, immediate) {

  let timeout

  return function (...args) {

    const later = () => {

      timeout = null

      if (!immediate) {

        fn.apply(this, args)

      }

    };

    clearTimeout(timeout)

    if (immediate && !timeout) {

      fn.apply(this, args)

    }

    timeout = setTimeout(later, wait)

  }

}

export function throttle(fn, limit = 250) {

  let wait = false

  return function (...args) {

    if (wait) {

      return

    }

    wait = true

    fn.apply(this, args)

    setTimeout(() => {

      wait = false

    }, limit)

  }

}

深拷贝数据

export function clone(data) {

  return JSON.parse(JSON.stringify(data))

}

判断是否为对象

export function isObject(v) {

  return Object(v) === v

}

判断是否为

export function isDate(v) {

  return Object.prototype.toString.call(v) === '[object Date]'

}

export function isRegexp(v) {

  return Object.prototype.toString.call(v) === '[object RegExp]'

}

判断是否为字符串

export function isString(v) {

  return typeof v === 'string'

}

错误函数提示

export function errAction(response) {

  this.$toast({type: 'danger', content: '网络或服务器异常,保存失败', time: 1500});

  throw new Error(response)

}

上一篇 下一篇

猜你喜欢

热点阅读