function throttle(method, delay) { let timer, args = arguments, start; return function fn() { let self = this; let now = Date.now(); if(!start){ start = now; } if(timer){ clearTimeout(timer); } if(now - start >= delay){ method.apply(self, args); start = now; }else { timer = setTimeout(function () { console.log(args,‘args‘) fn.apply(self, args); }, delay); } } }
function debounce(method,delay){ let _delay = delay || 0; let timer = null; return function(){ let args = arguments; timer && clearTimeout(timer); timer = setTimeout(()=>{ method.apply(this,args) },_delay) } }
原文:https://www.cnblogs.com/hill-foryou/p/11061224.html