js 函数

延迟执行

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)

posted @ 2022-10-04 13:13  刺头  阅读(20)  评论(0编辑  收藏  举报