const box=document.querySelector('#box') let i=1 function move(){ box.innerHTML=i++ } // 手写防抖函数,debounce(调用的函数,等待时间) function debounce(fun,t){ let timer return function(){ if(timer) clearTimeout(timer) timer=setTimeout(function(){ fun() },t) } } // box.addEventListener('mousemove',debounce(move,500)) // 手写节流函数,thorttle(调用的函数,等待时间) function thorttle(fn,t){ let timer=null return function(){ if(!timer){ timer=setTimeout(function(){ fn() timer=null },t) } } } box.addEventListener('mousemove',thorttle(move,500))
标签:function,box,防抖,节流,move,timer,手写 From: https://www.cnblogs.com/hs20011205/p/17248083.html