其他分享
首页 > 其他分享> > 高阶节流:`闭包` + `return数据` + `传参`

高阶节流:`闭包` + `return数据` + `传参`

作者:互联网

<script>
  var resultValue = "1";
  function throttle(fn) {
    console.log(arguments);
    let params = Array.from(arguments);
    params.shift();
    let res = arguments[arguments.length - 1];
    params.pop();
    let canRun = true;
    return () => {
      if (!canRun) return;
      canRun = false;
      setTimeout(() => {
        canRun = true;
        window[res] = fn.apply(this, params);
        console.log(resultValue);
        return res;
      }, 2000);
    };
  }
  function sayHi(value1, value2) {
    console.log(value1, value2);
    return "result=======";
  }

  var x = throttle(sayHi, 1, 2, "resultValue");
  setInterval(() => {
    x();
  }, 500);
</script>

标签:闭包,传参,return,log,canRun,params,arguments,console
来源: https://www.cnblogs.com/firefly-pengdan/p/14871059.html