js如何实现防抖(代码介绍)

2025年01月25日 建站教程

防抖动函数:

function debounce(fn,wait=50,immediate) {
 let timer;
 return function() {
   if(immediate) {
     fn.apply(this,arguments)
   }
   if(timer) clearTimeout(timer)
   timer = setTimeout(()=> {
     fn.apply(this,arguments)
   },wait)
 }
}

滚动防抖:

function realFunc(){
  console.log("Success");
}
//防抖
window.addEventListener('scroll',debounce(realFunc,500));

本文链接:http://so.lmcjl.com/news/22035/

展开阅读全文
相关内容