debounce防抖
1function debounce(fn, delay) {2 let timmer = null;3 return function () {4 timmer && clearInterval(timmer);5 timmer = setTimeout(() => {6 fn.call(this, arguments);7 }, delay);8 };9}
throttle节流
1function throttle(fn, delay) {2 let startTime = 0;3 return function () {4 let currentTime = new Date();5 if (currentTime - startTime > delay) {6 fn.call(this, arguments);7 startTime = currentTime;8 }9 };10}