4

JavaScript 防抖函数,绑定事件后事件对象传参问题

 2 years ago
source link: https://www.v2ex.com/t/879440
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

JavaScript 防抖函数,绑定事件后事件对象传参问题

  WNW · 5 小时 58 分钟前 · 472 次点击

明明事件对象 e 是作为的 debounce 中的第一个 fn 函数参数,为什么能够被 debounce 函数返回的匿名函数中 的...args 获取得到呢?实在想不通,不知道有哪位同学可以答疑解惑,不胜感激

// 防抖
function debounce(func, wait = 500) {

  let timeID;

  return function (...args) {

    if (timeID) clearTimeout(timeID);
    timeID = setTimeout(() => {
      func.apply(this, args);
    }, wait);
    
  }

}

let password = document.querySelector('[name="password"]');


// 明明事件对象 e 是作为的 debounce 中的第一个 fn 函数参数,为什么能够被 debounce 函数返回的匿名函数中
// 的...args 获取得到呢?实在想不通,不知道有哪位同学可以答疑解惑,不胜感激

password.addEventListener("keyup", debounce(function (e) { 
	console.log(222) 
}, 500));





About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK