节流最近用的有点多呢

105 阅读1分钟
/*
   * 节流 思路:
   * 先开启一个定时任务执行,定时任务完成后则清空,当再调用时,如果定时任务仍存在则不执行任何操作
   * */

let task = null;

function throttle(fn, space) {
  return function () {
    if(!task) {
      task = setTimeout( ()=> {
        task = null;
        fn.apply(this, arguments);
      }, space);
    }
  }
}
// 节流
throttle(showMsg500, 1000)(message);
function showMsg500(message){
  // do something...
  Message({
    message: message,
    type: 'error',
    duration: 5 * 1000
  })
}