SOURCE

console 命令行工具 X clear

                    
>
console
function throttle(fn, delay) {
  let timer = null; // 定义一个定时器
  return function (...args) { // 返回一个新函数
    if (!timer) { // 如果定时器不存在
      timer = setTimeout(() => { // 启动一个定时器
        fn.apply(this, args); // 执行函数
        timer = null; // 清空定时器
      }, delay);
    }
  };
}

function handleInput() {
  console.log('input changed');
}

// 创建一个防抖函数,延迟时间为 500ms
const debounceHandleInput = throttle(handleInput, 500);

const inputDom = document.createElement('input');

document.body.appendChild(inputDom);

// 绑定到 input 元素的 onchange 事件上
document.querySelector('input').addEventListener('input', debounceHandleInput);
<body></body>