lw-utils-package

1.0.1 • Public • Published

防抖函数

"效果" :单位时间内多次触发,只执行最后一次

// 参数1传入一个回调函数,参数2传入防抖生效时间
function debounce(callback, delay) {
  let timer = null;
  return function (...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      callback.apply(this, args);
    }, delay)
  }
}

节流函数

单位时间内只响应一次触发

// 参数1传入一个回调函数,参数2传入节流单位时间
function throttle(callback, delay) {
  let timer = null;
  return function (...args) {
    if (timer) return;
    timer = setTimeout(() => {
      callback.apply(this, args);
      timer = null;
    }, delay)
  }
}

深克隆

作用:完整复制一个对象,与源对象不会发生内存共享

// 传入需要克隆的对象,返回一个新对象
function deepClone(target) 

Package Sidebar

Install

npm i lw-utils-package

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

3.5 kB

Total Files

6

Last publish

Collaborators

  • coderlw