In the interview today, I asked the question of anti-vibration and throttling again. I thought this topic was too easy for me to write properly, but the interviewer said it needed to be implemented for the first time. Later, I thought that this operation might be really needed in practical application.

Function debounce(fn, delay) {let timer = null; Return function () {clearTimeout(timer) timer = setTimeout(() => {fn()}, Function debounce(fn, delay) {let timer = null; let count = 0; Let self = this return function () {let args= [...arguments] clearTimeout(timer) Count ++ fn. Apply (self,args) timer = setTimeout(() => {count = 0; }, delay) } else { count++ timer = setTimeout(() => { fn.apply(self,args) count = 0; }}}}, delay) / / test let a = debounce ((num) = > (the console. The log (num)), 1000) the window. The onclick = () = > {a} ()Copy the code

Let self = this, and then fn. Call (self,args). Have clear friends can comment section to communicate together.