美文网首页
工作常用函数封装

工作常用函数封装

作者: webj | 来源:发表于2018-04-23 18:28 被阅读0次

判断是否为数字

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)

}

相关文章

网友评论

      本文标题:工作常用函数封装

      本文链接:https://www.haomeiwen.com/subject/ddgllftx.html