1、创建26个大写字母

const alphabet = Array.from(new Array(26), (ele, index) => {
    return String.fromCharCode(65 + index);
});

2、获取当前时间戳

const now = +new Date();

3、防抖节流
防抖(debounce):某一时间段内只执行一次,多应用于search搜索联想,Window触发
resize事件
节流(throttle):间隔时间执行,多应用于鼠标不断点击mousedown,监听滚轮事件

function debounce(fn, delay) {
  let timer = null;
  return function () {
    let that = this;
    let _args = arguments;
    if(timer) {
      clearTimeout(timer);
    }

    timer = setTimeout(function() {
      fn.call(that, _args)
    }, delay)
  }
}
function throttle(fn, delay) {
  let prev = +new Date();

  return function() {
    let that = this;
    let _args = arguments;
    let now = +new Date();
    if(now - prev >= delay) {
      fn.call(that, _args);
      prev = +new Date();
    }
  }
}

风灵无畏
174 声望1 粉丝

愿你敲键盘,是真的喜欢,而不是生活所迫。走出多年,归来仍是少年


« 上一篇
vuex入门案例