throttle.js 583 B

123456789101112131415161718192021222324252627
  1. /*
  2. * @Descripttion:
  3. * @version:
  4. * @Author: qianlishi
  5. * @Date: 2021-12-11 14:48:27
  6. * @LastEditors: qianlishi
  7. * @LastEditTime: 2021-12-13 10:16:48
  8. */
  9. /**
  10. * 函数节流
  11. */
  12. export function _throttle(fn,delay){
  13. let timer
  14. let delay = delay || 1000; //一秒内触发一次
  15. return function(...args){
  16. const context = this
  17. let canExecute = !timer
  18. if(canExecute){
  19. fn.apply(context,args)
  20. }else{
  21. clearTimeout(timer)
  22. }
  23. timer = setTimeout(() => {
  24. timer = null
  25. }, delay);
  26. }
  27. }