节流(throttle) 就是说在短时间内,如果快速重复的点击,无法执行请求
<button id="btn"></button><script>function throttle(fn,delay){let timer;return function(){let self = thisif(timer) return;timer = setTimeout(()=>{fn.apply(self,arguments)timer = null},delay)}}function fn(){console.log('测试节流')}document.querySelector("#btn").addEventListener('click',throttle(fn,1000))</script>
立即执行
//防抖(合并版)function debounce_merge(fn, wait = 500, isImmediate = false) {var timerId = null;var flag = true;return function () {var context = thisvar args = argumentsif(timerId) return false;if (isImmediate) {if (flag) {fn.apply(context, args)flag = false}timerId = setTimeout(function () {flag = true}, wait)} else {timerId = setTimeout(function () {fn.apply(context, args)}, wait)}}}
