首页 > 其他分享 >js 函数

js 函数

时间:2022-10-04 13:22:31浏览次数:48  
标签:function setTimeout 函数 debounced js timer null log

延迟执行

function debounce(wait) {
  var timer = null;
  return function (fn) {
    if (timer !== null) {
      clearTimeout(timer);
    }
    timer = setTimeout(fn, wait);
  };
}

let debounced  = debounce(3000);

debounced(()=>{
	console.log("1");
})

setTimeout(()=>{
	debounced(()=>{
		console.log("2");
	})
},2000)

标签:function,setTimeout,函数,debounced,js,timer,null,log
From: https://www.cnblogs.com/yuyw/p/16753628.html

相关文章