// 防抖 规定时间内持续触发只执行最后一次操作 function debounce(fn, delay) { let timer = null; return function () { if (timer) clearTimeout(timer); timer = setTimeout(() => { fn.apply(this, arguments) }, delay) } } // 节流 规定时间内持续触发只执行第一次操作 function throttle(fn, delay) { let beginTime = 0; return function () { const triggle = new Date().getTime(); if (triggle - beginTime > delay) { fn.apply(this, arguments); beginTime = triggle; } } }